by Example

Variables


In Elixir, variables are bound to values,
via "=" (the match operator).



Unlike Erlang or other typical functional languages,
Elixir allows the same variable to be rebinded multiple times.







Variable names can start with an lowercase letter,
or underscore "_" and may contain alphnumeric characters.

To check for a match against a previously bounded variable,
use the carret symbol "^"

This will fail because n is bounded to 3 not 2







n = 3
IO.inspect(n)




name = "Joe Doe"
IO.inspect(name)

name = "Jane Doe"
IO.inspect(name)








try do
  ^n = 2
rescue
  error in MatchError ->
    IO.inspect(error, label: "Failed because")
end
$ elixir variables.exs
3
"Joe Doe"
"Jane Doe"
Failed because: %MatchError{term: 2}


Next: Modules