O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Function’s Body Is a Block

The doend block is one way of grouping expressions and passing them to other code. They are used in module and named function definitions, control structures…any place in Elixir where code needs to be handled as an entity.

However, doend is not actually the underlying syntax. The actual syntax looks like this:

 
def​ double(n), ​do​: n * 2

You can pass multiple lines to do: by grouping them with parentheses.

 
def​ greet(greeting, name), ​do​: (
 
IO.puts greeting
 
IO.puts ​"How're you doing, #{name}?"
 
)

The doend form is just a lump of syntactic sugar—during compilation it is turned into the do: form. (And the do: form itself is nothing special; it is simply a term in a keyword list.) Typically people use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required