for j in rand(1:100, 20)
so it complains about the missing end for the for-loop. So the syntax does already work, you just need an extra end. There’s no real way around this either since otherwise it would be ambiguous where which block ends.
So why isn’t end required in the generator expression [j for j in 1:100 if iseven(j) end]?
I mean, I know that the answer is “because it’s a generator expression and not an if block”, but if Julia can figure out that this is a generator expression and not an if block, then would it be dangerous to have a syntactical rule that says “if if appears on the same line as a for loop declaration, treat it like you would in a generator expression”?
(Again, I’m not trying to be snarky here, just trying to improve my understanding by getting a sense for what is or isn’t possible and why.)
The difference is that the generator expression is unambiguously delimited by the ]. Yes, in theory we could make this dependent on whether there’s a newline or a space before the if, but I don’t think it’s worth the inconsistency compared to how blocks are nested everywhere else. Contrary to Python, we generally try to avoid syntactically significant whitespace.