Minor thing but anyway — I just finished “if-else
” statement. That’s right, “if-else
” is not an expression, it is a statement, but if time shows expression is needed I will add it as an expression as well (I already have syntax in head). But first — statement:
if cond1 then ... else cond2 then ... else ... end
For some time I was wondering if I should introduce “else if
”, “elif
” or something else. As for the latter form (and its variations) I prefer to stick to English. I opted against the former, because I prefer to kill a chain of last end (it’s doable with no sweat), but this means there is a slight ambiguity when reading the code (not for compiler though). And presented form at each point there is double safe guard. Consider the tail:
else cond3 ... end
It is human error — but in order to fool compiler, you would have to add “then
” or semicolon. Thus double mistake.
As for middle section:
else cond2 ...
Again human error (dropped “then
”) but then you would have to add a semicolon (not mentioning error on not used expression outcome). Double mistake here is needed as well.
And there is no ambiguity when reading the code with inner “if
”, because there is no such instruction as “else if
”:
if cond1 then ... else if cond2 then ...
Despite formatting you don’t have to look down how many “ends
” close such code, because the second “if
” cannot be part of the first one.
OK, that’s it for what is done, one word about expression-like syntax (on hold):
a = if cond1 : simple_expr1 else cond2 : simple_expr2 else simple_expr3 end
I admit, I hate reading “if-else
” in any language I know, because of the formatting, and I seriously (no kidding) considered changing “if
” to “when
”.
a = when cond1 : simple_expr1 else cond2 : simple_expr2 else simple_expr3 end
Isn’t it a beauty?