I just tried the following code and was surprised that it failed:
julia> [0, -1.2 -1im, 0]
ERROR: syntax: missing separator in array expression
These two examples work:
julia> [0, -1.2 - 1im , 0]
3-element Array{Complex{Float64},1}:
0.0 + 0.0im
-1.2 - 1.0im
0.0 + 0.0im
julia> [0, -1im , 0]
3-element Array{Complex{Int64},1}:
0 + 0im
0 - 1im
0 + 0im
julia> versioninfo()
Julia Version 1.0.0
Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)
Platform Info:
OS: Linux (x86_64-unknown-linux-gnu)
CPU: Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.0 (ORCJIT, ivybridge)
With [0, -1.2 -1im, 0], it is interpreted as [0, -1.2, -1im, 0] with the second , missing.
Because -1 is preferentially parsed as a literal negative number, not a subtract of positive one, but - 1 is a subtract of positive one.
@elextr I see a problem of consistency. The following works:
julia> -1.2 -1im
-1.2 - 1.0im
but inside of an array, it does not. In Python it works in both cases. I would say that -a -ib should either be allowed everywhere or raise a syntax error everywhere. In case the Julia devs choose to leave it like it is, I have two suggestions:
1) The Julia docs should mention this inconsistency. In part, because many people are familiar with Python.
2) The error message should suggest that if you intended to write a complex number, you should either write it as an addition of a real part and a negative imaginary part or a subtraction of a positive imaginary part from a real part.
Arguing based on Python isn't very useful—they're different language with totally different syntaxes. In particular, Python doesn't have Julia's block matrix syntax or macros, which are the two places where Julia has space-sensitive syntax. In general, don't use asymmetrical spaces around infix operators like + and -. I agree that this should be more clearly documented; if it's in the manual, I can't find it.
Arguing based on Python isn't very useful
I did not mean to say that I expect Julia and Python to behave the same. I mentioned Python because I do expect many Python users to be surprised by this behavior in Julia.
In general, don't use asymmetrical spaces around infix operators like + and -
This is the kind of advice that should be written inside of a colored box in the documentation.
Its in the manual, but more like parenthetical stylistic advice rather than something that can affect parsing, see the paragraph above this
I'm not sure that qualifies as "documented". It should definitely be mentioned where the h/vcat syntax is introduced and in the section on calling macros.
Probably best if the experts write something that defines exactly what the space dependent syntax rules are, and then refer to it from all the above places and any other ones where the syntax has an impact.