Calling & (x, y) bitwise operator
After exploring Why does the + function work on tuples? I have the following question.
Can anyone explain to me why it Base.:&(1,2)
works but &(1,2)
fails? At the same time, Base.:|(1,2)
and |(1,2)
.
+3
source to share
1 answer
The reason is that &
as a unary operator it is a special form, as it is used in syntax ccall
(although this syntax is now deprecated ). Hence, it is &(1, 2)
parsed as Expr(:&, :(1, 2))
.
-
|
is not a unary operator, so it is|(1, 2)
parsed as1 | 2
a function call. -
+
and-
have special parsing rules so that+(1, 2)
u-(1, 2)
can be parsed as function calls with two arguments (otherwise they would be a function call with one argument in tuples, so at runtime).&
does not obey these rules as it is a special form and not a normal operator. -
Base.:&
is not parsed as an operator at all, but rather just a normal field-to-identifier reference. So there is no ambiguity here and it is parsed like a normal function call. Likewise, it is(&)(1, 2)
parsed like a normal function call because it is(&)
parsed like a normal identifier.
+7
source to share