Packages
Allows the use of `when` clauses on assignment statements as an alternative to using `if`, avoiding the need for boilerplate `else` clauses which return the original variables when the condition is `false`. For instance: ```elixir x = 1 when x == nil ```
Current section
Files
Jump to
Current section
Files
lib/assign_when.ex
defmodule AssignWhen do
@moduledoc """
Allows one to replace code like this:
x = if condition(), do: whatever, else: x
with code like this:
x = whatever when condition()
Exports no functions, just a macro. The macro does no validation,
but it does work on tuples and anything else that can be expressed
as THING = THING
"""
# Note this code is from Jose Valim from a private conversation I had with him.
defmacro unquote(:when)({:=, _, [left, right]}, condition) when true do
quote do
unquote(left) = if unquote(condition), do: unquote(right), else: unquote(left)
end
end
end