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
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
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
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.