question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

make 'then' optional in 'if' expressions?

See original GitHub issue

In 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:

  1. symmetry of the two branches of the if, and
  2. 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 conditionalBranches, 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 an if expression,
  • but only if we required you to put parens around anonymous functions, the else operator, and switches in the expression clause of a comprehension.

Is there any support here for making a change like that?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
phlopsicommented, Apr 21, 2018

Please, use the +1 or -1 reaction directly below the first post to express your opinion in a way, that’s easy to count. 😃

1reaction
CPColincommented, Apr 7, 2018

I support leaving it as if-then-else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Optional - If Else Statements - Stack Overflow
Writing with if-else statement is imperative style and it requires the variable car to be declared before if-else block. Using map in Optional...
Read more >
Replace your if..else if…/ nested if-not-null -then-get blocks ...
Described here is a step ahead, over this, on how to replace a specific if..else chaining with an elegant looking block using Optionals....
Read more >
Is it worth to use if-else statement as Java optional pattern?
My first instinct is that your code is a pretty “cool” way to rewrite if else statements, but I don't think it gains...
Read more >
Guide To Java 8 Optional | Baeldung
There are several ways of creating Optional objects. Note that we used the isPresent() method to check if there is a value inside...
Read more >
Optional binding and defer statements in iOS swift
Use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found