After investigating Why does the + function appear to work on tuples? I have the following question.
Could anyone explain me why Base.:&(1,2)
works but &(1,2)
fails?
At the same time Base.:|(1,2)
and |(1,2)
both work.
The reason is simply that &
as a unary operator is a special form, as it is used in the ccall
syntax (although this syntax is deprecated now). Hence &(1, 2)
is parsed as Expr(:&, :(1, 2))
.
|
is not a unary operator so |(1, 2)
is parsed as 1 | 2
, a function call.+
and -
have special case parsing rules so that +(1, 2)
and -(1, 2)
can be parsed as two-argument function calls (otherwise they would be one-argument function calls on tuples, which would error at runtime). &
is not subject to these rules as it is a special form, not an ordinary operator.Base.:&
is not parsed as an operator at all, but rather just an ordinary field reference to an identifier. So there is no ambiguity here and it's parsed like an ordinary function call. Similarly, (&)(1, 2)
is parsed as an ordinary function call because (&)
is parsed as just an ordinary identifier. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With