Multi-statement if/switch/do expressions
This is a very long thread but given my personal interest in where this proposal goes, I went through and read all of the first posts and most of the follow up posts, thought I did skim much of the later back and forth discussion that happened later in the thread.
I want to summarize what I’ve read so far so that people entering the discussion don’t feel like they necessarily have to read through everything to add to the discussion. As fair warning, my own opinion is intertwined heavily in this summary.
Out of the people who posted something, over a third of people are firmly against the proposal. Their main arguments against are:
- Multiline if expressions will generally encourage less readable code. Having multiline if expressions seems to facilitate the convenience of writing code, but since code is read more often then it’s written, I find this a compelling argument
- Side effects within expressions seem unintuitive.
- Counter arguments were given such as side-effects in function call expressions, getters, or even arithmetic operators.
- Adding a keyword to the language should have a higher standard and stronger motivation.
- There are already suitable alternatives such as imperative style variable declaration and immediately executed closure.
Adding to the people who out-right opposed, there were several who state that they were opposed to the pitch in it’s current form, and offered various proposals for what they would be in favor of, although each of their opinions is different from another.
Most of the arguments about multiline if expressions leading to poor and difficult to read code are mostly ignored. Instead, must of thread is suggesting alternatives to using “then” as a keyword. Of the people who expressed strong support or neutrality, almost all of them suggested an alternative to the use of “then”. I only found six people that became convinced that using “then” as a keyword was an acceptable solution, though I may have missed some.
Other than the use of “then” the three main alternatives discussed seem to be:
- Introducing a different keyword or operator/symbol. Dozen’s were suggested, but none seems to be more popular than “then”
- Using “return” contextually within if-expressions
- Using the bare last expression of the block.
Although each of these ideas has its supporters each of them met opposition as well.
Using “then” or an alternative keyword
No matter which keyword or symbol you use, they all seem to have the same problem. They all introduce the language feature in a way that is not familiar to any other programming language. As far as I’m familiar, no language that allows if-blocks as expressions utilizes a keyword.
Using “return” contextually
In a strong sense this breaks the expected meaning of return which historically and across almost all languages means that you are going to pop the call stack and return to the caller. However, we could fudge the way we think about that a bit by waving our hands and saying that if-expressions are syntactic sugar for immediately-executed closures. Although it would be unfamiliar to other languages, I think the meaning could be inferred fairly quickly by looking at surrounding code that would imply that the return was meant for the expression and not the containing function. In many cases it will be obvious when the expression type is different than the enclosing function type.
Using the bare last expression
This is one of the alternatives mentioned in the pitch. In the thread it’s the alternative that got the most traction from supporters, but it also has the strongest arguments against it in the context of Swift. I assume support from this comes from its simplicity and familiarity from other languages. However, it breaks what I believe is to this point a long-decided design decision in the language’s syntax. It has been decided that single line closures, functions, and getter do not need to use a “return” keyword, but longer blocks do. I think a lot of thought has already been put into that decision and using bare last expressions creates glaring inconsistencies within Swift. Other’s gave several reasons that it would be problematic. There are a few contexts where it adds additional ambiguity to the language. First, result build where bare expressions already have a meaning. And second, ambiguity with implicit static member look up on the last expression. The final point that a few people emphasized is the added difficulty for humans to read the code alluding to experience working with Scala which relies heavily on this feature.
My conclusion
There seems to be a fair amount of opposition so far to this pitch, although that is somewhat clouded by the lengthy discussion of alternative. Of the people the support the direction of this proposal, there doesn’t seem to be a settled direction for how to implement it in the context of Swift.
Counter proposals
There where two counter proposals of which I would be grateful to hear direct responses by the pitch’s author @Ben_Cohen
My own:
We should take a much smaller step by allowing single line do-catch-expressions as a way to make the language feel more consistent with was was added in SE-0380
Ethan Pippen's: @Pippin
I can’t do justice in explaining it, but here’s a link too his comment. Pitch: Multi-statement if/switch/do expressions - #208 by Pippin