Better error messages: Invalid Comprehension syntax

Consider this syntax error:

julia> S1 = [(.01 : .1 : 1.0);];

julia> S2 = [(.01 : .1 : 1.0);];

julia> [s1 +s2 for s1 in S1, s2 in S2]
ERROR: syntax: invalid comprehension syntax
Stacktrace:
 [1] top-level scope
   @ none:1

julia> [s1 + s2 for s1 in S1, s2 in S2]
10×10 Matrix{Float64}:
 0.02  0.12  0.22  0.32  0.42  0.52  0.62  0.72  0.82  0.92
 0.12  0.22  0.32  0.42  0.52  0.62  0.72  0.82  0.92  1.02
 0.22  0.32  0.42  0.52  0.62  0.72  0.82  0.92  1.02  1.12
 ⋮                             ⋮
 0.72  0.82  0.92  1.02  1.12  1.22  1.32  1.42  1.52  1.62
 0.82  0.92  1.02  1.12  1.22  1.32  1.42  1.52  1.62  1.72
 0.92  1.02  1.12  1.22  1.32  1.42  1.52  1.62  1.72  1.82

It turns out adding a space between + and s2 resolves the error.
Is there a way for Julia to guide the user to add a space between + and a variable etc?

PS: I’ve made this error a bunch of times…

It shouldn’t be an error. Seems to be a parsing bug.

2 Likes

Can someone guide me as to where in the Julia code this can be addressed?

Another valid form is

[s1+s2 for s1 in S1, s2 in S2]

But s1 +s2 breaks the parser, presumably because it looks like two expressions separated by a space. This is especially an issue since square brackets are also used for arrays, and the syntax for arrays is very space-sensitive.

2 Likes