Reputation: 569
Using Elixir V1.1.1 on OS X I am having problems getting my head around the Elixir function definition syntax.
Given a function definition:
def foobar(arg1, arg2), do: <<something>> end
It seems that in some cases the comma after the parenthesis is required, other times it is not. Likewise the colon after the 'do' and likewise the closing 'end'. I am sure I am vastly over-complicating the situation, but this seems like a weird setup for a programming language.
Under what circumstances are these elements required/optional?
Upvotes: 2
Views: 4758
Reputation: 25049
You've combined two forms of method definition there - the shorter one-line syntax and the longer do
/end
syntax.
If you have a one-line method, you can write like the following:
def foobar(foo, bar), do: baz
Note the comma, and no end
.
If you have a multi-line method, the syntax is slightly different:
def foobar(foo, bar) do
foo
bar
end
Note no comma and the placement of the end
.
Both types are described here: http://elixir-lang.org/getting-started/modules.html#named-functions (note the long form for Math.zero?/1
, and the shorter form under the 'Function capturing' heading)
Upvotes: 10