Counter-intuitive behaviour of the & function. Is it working as intended?

Hello Everyone,
I am a beginner to Julia and found the following behaviour of the & bitwise and operator rather perplexing.

When I type in the REPL something like:

julia> 1 & 3

Everything goes ok and I get an answer:

1

However, when I type:

julia> &(1, 3)

I get the following error:

ERROR: syntax: invalid syntax &(1, 3) around REPL[3]:1
Stacktrace:
[1] top-level scope at REPL[3]:1

I found this very counter-intuitive, since this syntax is perfectly valid for an operator like +.
So I decide to write the following:

julia> methods(&)

And I get:

16 methods for generic function “&”:

[1] &(a::BigInt, b::BigInt, c::BigInt, d::BigInt, e::BigInt) in Base.GMP at gmp.jl:518
[2] &(a::BigInt, b::BigInt, c::BigInt, d::BigInt) in Base.GMP at gmp.jl:517
[3] &(a::BigInt, b::BigInt, c::BigInt) in Base.GMP at gmp.jl:516
[4] &(x::BigInt, y::BigInt) in Base.GMP at gmp.jl:476
[5] &(a::Missing, b::Bool) in Base at missing.jl:159
[6] &(::Missing, ::Missing) in Base at missing.jl:158
[7] &(::Missing, ::Integer) in Base at missing.jl:161
[8] &(b::Bool, a::Missing) in Base at missing.jl:160
[9] &(x::Bool, y::Bool) in Base at bool.jl:40
[10] &(x::T, y::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} in Base at int.jl:308
[11] &(x::Integer) in Base at operators.jl:515
[12] &(x::T, y::T) where T<:Integer in Base at promotion.jl:394
[13] &(a::Integer, b::Integer) in Base at int.jl:918
[14] &(::Integer, ::Missing) in Base at missing.jl:162
[15] &(left::Base.AbstractCmd, right::Base.AbstractCmd) in Base at cmd.jl:245
[16] &(a, b, c, xs…) in Base at operators.jl:538

I found this really intriguing, since according to this message &(x, y) should be defined for integers. The same does not happen for the other bitwise operators | and \veebar. In those cases I do not get an error.

What is wrong? Is it some bug, is it an intended behaviour, or am I misunderstanding something?

(&)(1,3) works. I think this is just a parsing thing.

6 Likes

Thank you, that helps.

Also, its docstring (?&) mentions that one needs to

Add parentheses for function application form: (&)(x, y).

2 Likes

So it’s intentional and documented. That solves my question.

2 Likes