May 2018
Intermediate to advanced
412 pages
9h 3m
English
When we call sum.(2,3), it’s easy to assume we simply assign 2 to the parameter a and 3 to b. But that word, assign, should ring some bells. Elixir doesn’t have assignment. Instead it tries to match values to patterns. (We came across this when we looked at pattern matching and assignment.)
If we write
| | a = 2 |
then Elixir makes the pattern match by binding a to the value 2. And that’s exactly what happens when our sum function gets called. If we pass 2 and 3 as arguments, and Elixir tries to match these arguments to the parameters a and b (which it does by giving a the value 2 and b the value 3), it’s the same as when we write
| | {a, b} = {2, 3} |
This means we can perform more complex pattern matching when we call ...
Read now
Unlock full access