Ampersand Operator not consistent with help?

help?> &
search: &

&(x, y)

But if I try it, it doesn’t work:

julia> &((1==2),(1==1))
ERROR: syntax: invalid syntax &(Core.tuple(==(1, 2), ==(1, 1)))
Stacktrace:
[1] top-level scope at none:0

julia> &(true,true)
ERROR: syntax: invalid syntax &(Core.tuple(true, true))
Stacktrace:
[1] top-level scope at none:0

I guess I have lost the plot a bit… (I’m used to following ‘what’s on the tin’…)

Yes, that part of the docstring should be fixed (and similarly for |). But note that the examples there are correct. I made a PR,

https://github.com/JuliaLang/julia/pull/34688

If you insist on a prefix syntax, use (&)(true, true) etc.

2 Likes

Many thanks! Works for me!

You may close this (thanks!)

I could not find an open issue for this (and opening one seemed overkill, compared to just making a PR), so I am not sure what to close here. In any case, this PR should fix the docs.

Great - thanks! [I found out how to mark as ‘solved’]

Also, note that if you find errors like this, it is very easy to make a trivial PR on Github directly, see

I did not report it as an error, as there is so much about Julia that I find counter-intuitive that I just assumed it was me…

Interesting: & is bitwise and. && is logical and.

julia> (&)((1 == 2), (1 == 1))
false

and

julia> (&&)((1 == 2), (1 == 1))
ERROR: syntax: invalid identifier name "&&"
Stacktrace:
 [1] top-level scope at REPL[4]:100:

Now I am confused. I thought ‘&’ was the logical AND(?)

Help says && is the ‘short-circuiting’ logical AND(?). What if I just want
the ‘garden-variety’ logical AND?

That’s &

As the documentation says, & is the bitwise AND, meaning that it operates bit-to-bit on any pair of integers (plus missing values) – unlike &&, which only works on Bool numbers. For instance:

julia> 0b110 & 0b101
0x04

(with 0x04 being the same as binary 0b100).

But if you use it on Bool numbers, of course & will work as a logical AND, without short-circuit.

Yet another difference is that & is a broadcastable function, so you can use it with arrays, etc. The short-circuit operator only works with a single pair of logical values.

Isn’t it weird that Base.:& is a function, but there is no Base.:&& (the boolean short-circuiting operator)? Is it handled as a keyword?

Yes:
https://docs.julialang.org/en/v1/base/math/#&&

2 Likes

Short-circuiting means the second part of the statement is guaranteed to only be evaluated if the first one evaluates as true. This is not something that is possible with regular functions and therefore has to be special cased at a compiler level. Because of that, && and || get their own expression types and are not call expressions, like most infix operators.

2 Likes

Is there a reason why &(a,b) should be a syntax error when +(a,b) is allowed?

Otherwise it would seem more logical to make it work as advertised, rather than adjusting the documentation.

1 Like

& was once used as a unary operator in ccall and therefore had to be parsed differently. That use was deprecated for Julia 0.7 and according to deprecate `&x` syntax in ccall, replace with Ref · Issue #6080 · JuliaLang/julia · GitHub the plan was to change the parsing for 1.0 but it seems that didn’t happen. I’ve opened an issue to revisit this: Parse `&x`as a call expression · Issue #34693 · JuliaLang/julia · GitHub.

3 Likes