Reputation: 9193
I'm trying to wrap my head around Elixir and am confused about the following
def fun_myfunc(clause, do: expression) do
Specifically I'm a bit confused about the do: expression
part. Is this a parameter being passed into the function, and also why is there a :
. Is do
a keyword in this context ?
This also gives rise to the following, question on Phoenix
. If I look at the pipeline function https://hexdocs.pm/phoenix/Phoenix.Router.html#pipeline/2 I see the following signature pipeline(plug, list)
. What is list
in this context and how do I infer it by looking at the documentation. I know that they have given examples where list
looks like a bunch of expressions, but how do I gather that just by looking at the documentaion.
Upvotes: 1
Views: 140
Reputation: 23091
do: expression
is a keyword list. This is syntax sugar in two forms:
[]
key: value
instead of the equivalent {:key, :value}
Both of these are documented in the above link.
The function is equivalent to:
def fun_myfunc(clause, [{:do, expression}]) do
The :do
is simply an atom. It's up to the fun_myfunc
to decide how to handle it.
I suspect you are actually looking at a macro defined with defmacro
and misquoted it in your question. This is also how the if
macro works, for example.
if foo do bar else baz
is equivalent to if(foo, do: bar, else: baz)
, which is equivalent to if(foo, [{:do, bar}, {:else, baz}])
.
As for the Phoenix part, that should be a separate question.
Upvotes: 1