Extra token, on same line as for loop start, is valid?

Perhaps it would have made sense to require semicolons. I don’t know why it was done this way, there’s probably a good reason. Several other keywords work this way too, such as if and while. However, let requires a semicolon:

julia> let a = 3 println(a) end
ERROR: syntax: let variables should end in ";" or newline

What’s more surprising to me is that not even a space is required before an end statement. This is perfectly valid:

julia> a = 3; while a > 0 println(a); global a-=1end
3
2
1

As for linter and warnings, looking again at the code that troubled you: for i in 1:3 4, it might be more effective to detect and warn about that independent 4, than requiring semicolons after keywords. (Or are there cases where a single number like that would make sense, unless when used as a return value?)

What I think can be said with certainty is that changing this would break an enormous amount of code, and would likely have to be done over a series of releases (like v0.7 and v1.0). I have used this syntax many times, especially when answering questions on this forum to keep my replies more compact – here’s an example:

@btime (s = 0.0; @simd for i in 1:10^9 s += i * 1.23 end; s)

There’s also plenty of existing code out there using this syntax, here’s an example in Julia’s sparse matrix library:

for i in (k + 2):(n + 1) colptr[i] = (k + 1) end
1 Like