Point Circle Radius Rectangle Width Height
Shape
Abstract class
drawAt(x,y)
Point
drawAt(x,y) { ...}
Shape
Abstract class
drawAt(x,y)
Circle
Attribute: radius
drawAt(x,y) { ...}
Rectangle
Attributes:
width, height
drawAt(x,y) { ...}
Point Circle Radius Rectangle Width Height
Point Circle Radius Rectangle Width Height
type Shape =
Point | Circle Radius | Rectangle Width Height
type Shape =
Point | Circle Radius | Rectangle Width Height
type alias Radius = Float
type alias Width = Float
type alias Height = Float
type Shape =
Point | Circle Radius | Rectangle Width Height
type alias Radius = Float
type alias Width = Float
type alias Height = Float
Equivalent:
type Shape =
Point | Circle Float | Rectangle Float Float
type Shape =
Point | Circle Radius | Rectangle Width Height
type alias Radius = Float
type alias Width = Float
type alias Height = Float
Equivalent:
type Shape =
Point | Circle Float | Rectangle Float Float
Algebraic Data Type !
myRect = Rectangle 10 20
bigCircle = Circle 420000
drawAt (x, y) shape =
case shape of
Point ->
...
Circle radius ->
...
Rectangle width height ->
...
myDrawing1 = drawAt (5, 7) (Circle 10)
myDrawing2 = drawAt (10, 5) myRect
R. I. P.
Bob
tombstone =
(Rectangle 50 50)
tombstone =
Union
(Rectangle 50 50)
(Circle 25)
shift
tombstone =
Union
(Rectangle 50 50)
(Circle 25)
(0, 25)
-- the circle is
-- shifted by 25
-- vertically
shift
type Shape
= Point
| Circle Radius
| Rectangle Width Height
| Union Shape Shape Shift
type alias Shift =
(Float, Float)
And more:
Todo list
Appeler Bob
Lire H2G2
Tester Elm
New
3 elements
Todo list
3 elements
New
Title
Appeler Bob
Lire H2G2
Tester Elm
Todo list
3 elements
New
NumberOfElements 3
Title
Appeler Bob
Lire H2G2
Tester Elm
Todo list
3 elements
New
New
Title
Appeler Bob
Lire H2G2
Tester Elm
NumberOfElements 3
Todo list
3 elements
New
Title
New
type StaticText
= Title
| NumberOfElements Int
| New
Appeler Bob
Lire H2G2
Tester Elm
NumberOfElements 3
renderEnglish text =
case text of
Title ->
"Todo list"
NumberOfElements n ->
toString n ++ " elements"
New ->
"New"
renderEnglish text =
case text of
Title ->
"Todo list"
NumberOfElements n ->
case n of
0 ->
"You have nothing to do!"
1 ->
"One more thing to do!"
_ -> --default case
toString n ++ " elements"
New ->
"New"
renderFrench text =
case text of
Title ->
"À faire !"
NumberOfElements n ->
case n of
0 ->
"Tu n'as rien à faire !"
1 ->
"Une seule chose à faire !"
_ -> --default case
toString n ++ " élements"
New ->
"Nouveau"
À faire !
Nouveau
3 élements
Appeler Bob
Lire H2G2
Tester Elm
(non-exhaustive)
FULL SUPPORT
ML-family
Haskell, Elm, PureScript, ReasonML, OCaml, ...
Rust
Scala
PARTIAL SUPPORT
TypeScript
Define ADTs: Ok Pattern matching:
... hack it yourself!
EMULATION
All languages!
(not recommended)