Pitch: Multi-statement if/switch/do expressions

I'm fan of this! Overall this pitch seems really thought out, so +1 from me, no notes.

I always thought do statements should be expressions just like if & switch now are. It's a very common pattern in ruby to use the similar begin blocks to compress multiple lines into an expression. I like that do would now supplant the weird immediate closure nonsense.

I think the use of some keyword is a good call and feels very in keeping with Swift's overall style. I think it's a readability enhancement compared to Ruby, as mentioned.

On the bike-shedding question

I just wanted to note that Ruby also has an interesting (if underused) feature where break can also "take" a value just like return statements. So, you can do either of these:

value = if true 
  break 42
else
  0 
end

Taking that inspiration for Swift: we could consider reusing break statements for the same purpose as pitched for then. The up side is it's an existing keyword so most of the contextual keyword parsing difficulties become a non-issue. Currently break usage is only permitted in if or do when one of the statements is labelled, so I think the parsing would be easy enough.

here: if .random() {
  // labeled statements weren't even mentioned in the pitch
  // so I figure this is a non-issue
  break here
}
return if .random() {
  let here = 42
  break here // clear enough to me!
} else {
  break 0
}

I don't have a strong opinion on this. I think then is a fine choice. But I'm curious what others think about using break as an alternative spelling.

6 Likes