This is something I have wished for ever since implicit returns were introduced.
However, I see it as purely a generalization of the current rules for implicit returns, to allow branching using control flow statements, rather than just treating control flow blocks as expressions universally.
IOW I’m not a huge fan of things like using a switch on the right hand of an assignment expression (let result = switch value { … }
), but I do wish I could use implicit returns in control flow as a generalization of the current implicit return rules.
[Note: I most often find myself wanting this when switching over self
on an enum.]
enum Pet {
case dog, cat, bird, other(String)
var description: String {
switch self {
case .dog: "Dog"
case .cat: "Cat"
case .bird: "Bird"
case .other(let petName): petName
}
}