Elm allows you to define polymorphic functions.
Parametric polymorphism is when a function can be applied to elements of any type:
f : (a, b) -> (b, a)
f (x, y) = (y, x)
Ad-hoc polymorphism is when a function can be applied to elements of certain types:
g : appendable -> appendable -> appendable -> appendable
g x y z = x ++ y ++ z
h : number -> number -> number
h x y = (x + 2) * y
Variables of type number
and appendable
are special because they are a subset of all types of Elm. List
and String
are appendable
, while Float
and Int
are the types of numbers.
hasPlus
, List
, String
, Float
Int
, , , x + y
y + x
, , ...