Use keyword lists for ‘options’-style parameters that contains multiple key value pairs:
def myfunc(arg1, opts \\ []) do
# Function body
end
We can call the function above like so:
iex> myfunc "hello", pizza: true, soda: false
which is equivalent to:
iex> myfunc("hello", [pizza: true, soda: false])
The argument values are available as opts.pizza
and opts.soda
respectively.
Other Examples
Ecto’s query language uses this pattern:
query = from p in EctoBlog.Post, where: p.id == post_id
You can probably guess by now, but the above code is actually:
query = from(p in EctoBlog.Post, [where: p.id == 1])
Yes, the second parameter of the from
function is a keyword list.