julia> [ 2*x + 6 for x in 1:3]
3-element Vector{Int64}:
8
10
12
julia> "above as expected, now take away the blank before the 6"
"above as expected, now take away the blank before the 6"
julia> [ 2*x +6 for x in 1:3]
ERROR: ParseError:
# Error @ REPL[10]:1:22
[ 2*x +6 for x in 1:3]
# ╙ ── unexpected `]`
Stacktrace:
[1] top-level scope
@ none:1
This is essentially the same as the whitespace effects you asked about in Whitespace effects in x+1 … Julia is sensitive to whitespace in some contexts, especially with the +
operator (which can be either prefix or infix) and especially inside [...]
brackets (which can be either comprensions or array literals, with special whitespace-based syntax for matrix literals).
It’s safer to give an error in cases where the parsing is ambiguous. That being said, the error message here could probably be clearer.
PS. See PSA: how to quote code with backticks … I’ve fixed it for you above.
PPS. I’ve edited your post title to make the subject of this thread clearer.
And, as in Whitespace effects in x+1 - #2 by goerz : Whenever an element of a Vector or Array is some complex expression, put parentheses around that expression. Always err on the side of more parentheses!
As a side note, there is an ongoing discussion here: Add `strict` mechanism for opting into stricter subsets of the language · Issue #54903 · JuliaLang/julia · GitHub about optionally disallowing various undesirable language features. One of the topics has been spacing which disagrees with the precedence rules, like: a * b+c * d
and a ^ b*c
.