Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling &(x,y) bitwise operator

Tags:

syntax

julia

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.

like image 262
Bogumił Kamiński Avatar asked Feb 05 '23 17:02

Bogumił Kamiński


1 Answers

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.
like image 171
Fengyang Wang Avatar answered Feb 14 '23 05:02

Fengyang Wang