A more complicated case of the example below caused me to spend a long time debugging a small typo. In the code below, there is a syntax error ('=' instead of ':') in the third line of the function. However, when this function is run in 0.4.6 (StrangeError.fn(1,2,3,4,5)), the following error message results. Note that the error message refers to the second line in the function, that is, the first place in the code where a + operation is used, not the line with the syntax error.
julia> StrangeError.fn(1,2,3,4,5)
ERROR: UndefVarError: + not defined
in fn at C:\Users\vavasis\ownCloud\Documents\Katerina\cohesive\conic_jl\test_misplaced_eq.jl:4
module StrangeError
function fn(a,b,c,d,e)
u = a + b
c[a + b = c + d : e] = 0
return u
end
end
It seems this issue is a combination of several features of Julia:
So apparently this is not a bug in Julia, and yet some aspect of this code should really trigger a warning to help prevent Julia programmers from pulling their hair out (not a problem for me since I am already bald).
Your analysis is correct. If anything, I think we should further restrict where assignments are allowed to occur. In particular it would seem to be good to restrict function definitions to statement position.
What about this case:
function f(x)
g(y) = x + y
end
Technically, that's not statement position.
Good point. Should be disallowed in argument position instead.
FWIW, error message is now: "ERROR: UndefRefError: access to undefined reference".
In response to @KristofferC 's "FWIW", my answer is: "not much!". For a more in-depth complaint about this, please see:
Most helpful comment
Your analysis is correct. If anything, I think we should further restrict where assignments are allowed to occur. In particular it would seem to be good to restrict function definitions to statement position.