Space-sensitive parsing confusion

This is confusing.

julia> 1 + 2
3

julia> 1 +2
3

julia> [1 + 2]
1-element Array{Int64,1}:
 3

julia> [1 +2]
1×2 Array{Int64,2}:
 1  2

Is there a place in the manual where I can find this behavior described?

Several operators are parsed as unary when immediately adjacent to a number or variable (the operand), but binary otherwise. I don’t think there is any explicit description like the examples you give, but the behavior follows from the operator definitions: http://docs.julialang.org/en/release-0.5/manual/mathematical-operations/

An extra line or two in the manual might be useful since this has certainly come up before.

(The other operators with dual behavior are + - $ & ~. See here)

1 Like

Julia understands that you want to perform a binary operation when the infix operator (a) is not separated from the operands, or (b) is separated from both operands by a space, or (c) is separated from the second operand by a space:

  julia> 1+2, 1 + 2,  1+ 2, [1+2], [1 + 2], [1+ 2]
  3, 3, 3, [3], [3], [3]

When the first operand is separated from the operator by a space but the second operand is not separated from the operator by a space, then what happens inside brackets is different than before. Inside of brackets, the space character acts as a synonym for the column separator character, the semi-colon.

julia> [1,2,3]
3-element Array{Int64,1}:
1
2
3

julia> [1;2;3]
1×3 Array{Int64,2}:
1 2 3
julia> [1 2 3]
1×3 Array{Int64,2}:
1 2 3
1 Like

Thanks! That make sense.