π³
π³
π³
π³
Walking though the Elm woods
Searchβ¦
π³
π³
π³
π³
Walking though the Elm woods
Introduction
Structure of the book
Frequently asked questions
How can different types share data?
How to break Dependency Cycles?
How to structure an Elm project?
How to turn a Msg into a Cmd Msg?
How to update nested Records?
What are comparable types?
Why are Booleans bad?
π
Future topics
Recipes
Writing a Single Page Application
Making impossible states Impossible
Designing Elm package APIs
π
Future topics
Frameworks and packages
elm/parser
mdgriffith/elm-ui
π
Future topics
Powered By
GitBook
How can different types share data?
Question
Solution
Alternative Solution
1
type Animal
2
= Dog DogData
3
| Bird BirdData
4
5
type alias DogData =
6
{ name : String
7
, runningSpeed : Float
8
}
9
10
type alias BirdData =
11
{ name : String
12
, wingSpan : Float
13
}
14
β
15
{-| This function should work for all Animals
16
-}
17
getName {name} =
18
name
Copied!
1
type alias Animal =
2
{ name : String, data : AnimalData}
3
β
4
type AnimalData
5
= Dog DogData
6
| Bird BirdData
7
8
type alias DogData =
9
{ runningSpeed : Float
10
}
11
12
type alias BirdData =
13
{ wingSpan : Float
14
}
15
β
16
getName : Animal -> String
17
getName {name} =
18
name
Copied!
1
type Animal =
2
Dog DogData
3
| Bird BirdData
4
β
5
type alias AnimalData species =
6
{species| name : String}
7
8
type alias DogData =
9
AnimalData {runningSpeed:Float}
10
β
11
type alias BirdData =
12
AnimalData {wingSpan:Float}
13
β
14
getName : AnimalData species -> String
15
getName {name} =
16
name
Copied!
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
β
β
Previous
Structure of the book
Next - Frequently asked questions
How to break Dependency Cycles?
Last modified
2yr ago
Copy link
Contents
Question
Answer
Further Reading