# How can different types share data?

{% tabs %}
{% tab title="Question" %}

```
type Animal
    = Dog DogData
    | Bird BirdData
    
type alias DogData =
    { name : String
    , runningSpeed : Float
    }
    
type alias BirdData =
    { name : String
    , wingSpan : Float
    }

{-| This function should work for all Animals
-}
getName {name} =
    name
```

{% endtab %}

{% tab title="Solution" %}

```
type alias Animal =
    { name : String, data : AnimalData}

type AnimalData
    = Dog DogData
    | Bird BirdData
    
type alias DogData =
    { runningSpeed : Float
    }
    
type alias BirdData =
    { wingSpan : Float
    }

getName : Animal -> String
getName {name} =
    name
```

{% endtab %}

{% tab title="Alternative Solution" %}

```
type Animal =
    Dog DogData
    | Bird BirdData

type alias AnimalData species =
    {species| name : String}
    
type alias DogData =
    AnimalData {runningSpeed:Float}

type alias BirdData =
    AnimalData {wingSpan:Float}

getName : AnimalData species -> String
getName {name} =
    name
```

{% endtab %}
{% endtabs %}

## Question

How can I write one function for all animals, that returns just the `name`.

## Answer

Put the common data, in our case the name, all in one case and separate it from the differing data.

## Further Reading

* 👥**Thread:** [Passing accessors to functions](https://www.reddit.com/r/elm/comments/aq69vq/passing_accessors_to_functions/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://orasund.gitbook.io/elm-cookbook/frequently-asked-questions-1/how-can-different-types-share-data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
