make 'then' optional in 'if' expressions?
See original GitHub issueIn practice, I think I find then
in if
expressions significantly more annoying than I thought I would. And I’ve heard at least some complaints about it. The reason for requiring then
was:
- symmetry of the two branches of the
if
, and - to help resolve a grammatical ambiguity with the syntax of comprehensions.
I now no longer care about 1. I do still care about 2, but I must admit that we use comprehensions much less than I expected, and if
expressions much more, so I guess I’m prepared to sacrifice some convenience in comprehensions in order to get a little convenience in if
expressions. (It’s somewhat a tough call, however.)
The issue with comprehensions is the interpretation of the following code:
for (x in xs) if (fun(x)) x else 0
Does this mean:
for (x in xs) if (fun(x)) (x else 0)
Or:
for (x in xs) (if (fun(x)) x else 0)
The requirement for then
neatly resolved that ambiguity in favor of the first interpretation. However, there was always the option of resolving it in the opposite direction, interpreting (if (fun(x)) x else 0
as an if
expression instead of a clause of the comprehension.
The way to do that is to specify that the expression clause of a comprehension is not a full expression, but rather what the grammar calls a conditionalBranch
. You can write almost any expression in a conditionalBranch
except:
- anonymous functions
then
/else
operators- assignments
switch
expressions
Currently the branches of an if
, switch
, or let
are conditionalBranch
es, but the expression clause of a comprehension is a full functionOrExpression
.
So what this boils down to is, if I recall everything correctly:
- we could let you leave out the
then
in anif
expression, - but only if we required you to put parens around anonymous functions, the
else
operator, andswitch
es in the expression clause of a comprehension.
Is there any support here for making a change like that?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:23 (12 by maintainers)
Top GitHub Comments
Please, use the +1 or -1 reaction directly below the first post to express your opinion in a way, that’s easy to count. 😃
I support leaving it as if-then-else.