I learned that &(X.f/1) denotes the use of the capture operator (&) on a function f, which is defined inside the module X, and that f has arity 1.
I think I understood the semantic of this capturing, but it is the syntax which troubles me.
Looking at just X.f/1, this could also be understood as the result of the division of an niladic function X.f, by 1.
According to which rule is / understood here as a notation for arity instead of a division operator? Does the capture operator introduce a specific syntactic scope, which interprets the slash only as arity denotation?
(To clarify: With syntacic scope, I mean something similar in spirit as in shell programming (zsh, bash), where i.e. ((a+b)) is understood due the inclusion inside ((....)) as the addition of the shell variables a and b, without the need to write ...$a + $b...)
So, how does the compiler know, that / does not mean division here?
Yes. This actually happens in here in
:elixir_expandwhere the capture operator is handled, then most of the work gets delegated to:elixir_fn.capture/4.It is important to note that there is no distinction from a regular division at the AST level (you can confirm it in IEx with
quote do: foo/1andquote do: &foo/1). The compiler will simply interpret it differently when handling the capture.Here is an example to illustrate the point: consider
&:math.pi/&1. This is a valid function, equivalent tofn x -> :math.pi() / x end. Because the first clauses of:elixir_fn.capture/4won't match (Ais not an integer), it will consider/as a regular division expression, try to find an&1arg (and succeed).Finally, the error message from an invalid capture explains the logic well: