Bitwise and does not parse as function call

This does not seem consistent to me:


$ ./julia 
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0-dev.1889 (2017-01-04 09:08 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 888f6b5* (0 days old master)
|__/                   |  x86_64-linux-gnu

julia> 1 & 2
0

julia> &(1, 2)
ERROR: syntax: malformed expression

And julia help support this view:

help?> &
search: &

  &(x, y)

  Bitwise and.
 [ ... ]
julia> (&)(1,2)
0
2 Likes

It looks like the documentation doesn’t match the behavior. This seems to be a known issue with parsing &: https://github.com/JuliaLang/julia/issues/11521

I think the offending line in the documentation is here: (edited, see @tkelman’s post below) https://github.com/JuliaLang/julia/blob/75cbac32760e90645d579fac877155eb571eee40/base/docs/helpdb/Base.jl#L843

The best way to get this fixed, if you’re interested in helping, would be to open an issue on github (or, better yet, a pull request with a fix).

When you link to lines of code on github, it’s best to hit y first. Github will refresh the page but with the current sha of the file you’re looking at inserted into the url. That way even if the file changes on master, like that file just did, the line of code you’re referring to will remain what the link takes you to.

Cool, thanks! Will do. I’ll edit that comment for posterity.

Perhaps the docstring could have the infix form x & y, and just mention that to refer to the function, use (&).

Edit: Removed nonsense

Thanks @rdeits and @Tamas_Papp. I just created the pull request.

While on the subject. It still is not clear to mer why & is different from e.g. |. From the referenced discussion Cannot define & · Issue #11521 · JuliaLang/julia · GitHub it seems like & used to be a unary operator and that legacy is still haunting us ?

Some time ago I attempted to write a macro assembler in julia and needed post increment and decrement of registers. I found then that I could define a function named ++ but not --, which seemed arbitrary. None of those are julian functions and + and - are similar in all relevant aspects, afaik. But (from a fresh julia):

julia> ++
ERROR: UndefVarError: ++ not defined

julia> –
ERROR: syntax: invalid operator “–”

AFAIK & was part of the old ccall syntax.

3 Likes

++ was added so that people could use it for a generic concatenation operator (or at least for string concatenation) (a la Haskell), to replace the operator punning of the multiplication operator * for string concatenation.
That’s why only ++ is parsed, and not --.
Julia uses += 1 and -= 1 instead of ++ and -- for pre-increment and pre-decrement.
Note: for better type-stability, you might be better with something like: x += one(typeof(x))
There’s not really anything for post increment/decrement though AFAIK.