In Elm, values are declared by writing a name, an equals sign, and then the actual value:
someValue = 42
Functions are also values, with the addition of taking a value or values as arguments. They are usually written as follows:
double n = n * 2
Every value in Elm has a type. The types of the values above will be inferred by the compiler depending on how they are used. But it is best-practice to always explicitly declare the type of any top-level value, and to do so you write a type signature as follows:
someValue : Int
someValue =
42
someOtherValue : Float
someOtherValue =
42
As we can see, 42
can be defined as either an Int
or a Float
. This makes intuitive sense, but see Type Variables for more information.
Type signatures are particularly valuable when used with functions. Here's the doubling function from before:
double : Int -> Int
double n =
n * 2
This time, the signature has a ->
, an arrow, and we'd pronounce the signature as "int to int", or "takes an integer and returns an integer". ->
indicates that by giving double
an Int
value as an argument, double
will return an Int
. Hence, it takes an integer to an integer:
> double
<function> : Int -> Int
> double 3
6 : Int