- Single quotes vs double quotes
A lot of modern programming languages, like ruby, javascript and python, will treat'string'
the same as"string"
. In Elixir, however, it is not the case
> 'string' == "string"
false> IEx.Info.info('string')
... {"Data type", "List"} ...> IEx.Info.info("string")
... {"Data type", "BitString"} ...
2. function clause for idiomatic elixir
defmodule AnimalSoundEmitter do
def emit(:dog), do: :bark
def emit(:cat), do: :meow
def emit(:snake), do: :hiss
def emit(:lion), do: :roar
end
3. guard clause
defmodule Calculator do
def plus(a, b) when is_number(a) and is_number(b), do: a + b
def minus(a, b) when is_number(a) and is_number(b), do: a - b
def multiply(a, b) when is_number(a) and is_number(b), do: a * b
def divide(a, b) when is_number(a) and is_number(b), do: a / b
end
4. assignment vs matching
assignment variable < is on the left side of the expression
matching < no error throws
matching with destructing, from Common Lisp
can match just a part of values
5. eex treats if/for as an expression
in ruby erb
, no =
applies before if/for
<% if true %>
This is correct
<% else %>
This is not
<% end %>
in elixir eex
, a =
is required
<%= if true %>
This is correct
<% else %>
This is not
<% end %>
Use haml
, if you do not like eex