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
# Notes for Julia Contributors
Hi! If you are new to the Julia community: welcome, and thanks for trying Julia. Please be sure to respect our [community standards](https://julialang.org/community/standards) in all interactions.
If you are already familiar with Julia itself, this blog post by Katharine Hyatt on [Making your first Julia pull request](https://kshyatt.github.io/post/firstjuliapr/) is a great way to get started.
## Learning Julia
[The learning page](https://julialang.org/learning) has a great list of resources for new and experienced users alike.
## Before filing an issue
- Reporting a potential bug? Please read the "[How to file a bug report](https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#how-to-file-a-bug-report)" section to make sure that all necessary information is included.
- Contributing code? Be sure to review the [contributor checklist](https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#contributor-checklist) for helpful tips on the tools we use to build Julia.
- Library feature requests are generally not accepted on this issue tracker. New libraries should be developed as [packages](https://julialang.github.io/Pkg.jl/v1/creating-packages/). Discuss ideas for libraries at the [Julia Discourse forum](https://discourse.julialang.org). Doing so will often lead to pointers to existing projects and bring together collaborators with common interests.
## Contributor Checklist
This file has been truncated. show original
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?
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?
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
Per
February 7, 2020, 8:28am
15
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