when is an operator - it behaves very similar to operators like == or in.
iex(1)> quote(do: 1 when 2)
{:when, [], [1, 2]}
iex(2)> quote(do: 1 == 2)
{:==, [context: Elixir, import: Kernel], [1, 2]}
In case of def, the left side (of the operator) is the call and right the
guards.
Michał.
On 9 Mar 2017, 12:42 +0100, Brian Cardarella <[email protected]>, wrote:
> If I quote the following:
>
>
> Code.string_to_quoted """
> if foo do
> "true"
> else
> "false"
> end
> """
>
> I get the following AST:
>
> {:if, [line: 1], [{:foo, [line: 1], nil}, [do: "true", else: "false"]]}
>
> Which makes sense to me as the last node is a list. However, `when` seems to
> follow different rules and I'm not following exactly how Elixir parses it
> from the AST:
>
> Code.strig_to_quoted """
> def foo(bar) when bar == 1 do
> "baz"
> end
> """
>
> {:def, [line: 1],
> [{:when, [line: 1],
> [{:foo, [line: 1], [{:bar, [line: 1], nil}]},
> {:==, [line: 1], [{:bar, [line: 1], nil}, 1]}]}, [do: "baz"]]}
>
> `when` is not in that last list, but is now the first element in the values
> for `:def`
>
> Can someone point me to examples of how Elixir is parsing `when` within the
> source? I dug around in Kernel which brought me to elixir_def but got stuck
> there.
>
> Use case: I am experimenting with writing some macro functions that could
> take guard clauses.
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/43a58eef-5cd5-42b5-bf6d-8d496914a07b%40Spark.
For more options, visit https://groups.google.com/d/optout.