Variables
Variables give names to values so you can reuse them. In Elixir they follow snake_case style: they start with a lowercase letter, and words are separated by underscores.
iex> length = 10 (1)
10
iex> width = 23
23
iex> room_area = length * width
230
| 1 | The = operator binds the value 10 to the variable length. IEx
prints the value that was just assigned, which is why you see 10 on
the next line. |
Names that start with an uppercase letter are reserved for modules and
other top-level identifiers. Trying to use one as a variable produces a
MatchError:
iex> RoomWidth = 2
** (MatchError) no match of right hand side value: 2 (1)
| 1 | The MatchError looks cryptic for now, but it will make sense once
we cover the match operator.
The short version: = is not "assign", it is "match", and it behaves
differently when the left-hand side cannot be a variable. |
|
Pick descriptive names
|