[Pitch] Shorthand switch case expression

Apologies if this has been brought up and I'm just using the wrong search terms, but in my day job I work with C# a lot, and in C# 8 we got a new form of switch expressions that I would love to see added to Swift.

The idea is rather than explicitly adding return to each case line in circumstances where you only have one item for each case, you could use a shorthand to imply return. I use this in SwiftUI already like:

switch someEnum {
case .firstExample:
    Text("first view")
case .secondExample:
    Text("second view")
}

but regularly find myself wishing I could do the same elsewhere (particularly with enums where most of my usage involves single-line returns anyway).

I'm sure I'm not doing it justice, so I highly recommend reading about it to get a better idea of how useful this can be. It saves a lot of unnecessary return statements on each line when you just need to return some one-liner.

:v:

4 Likes

The core team also acknowledges that accepting [SE-0255: Implicit returns from single expression functions] may encourage follow-on proposals that generalize it further, such as converting if / switch statements into expressions, or allowing elision in multi-statement bodies. Accepting this proposal doesn't necessarily endorse or rule out those future directions.

6 Likes

Glad to see I'm not alone at least! It would certainly keep things more concise :slightly_smiling_face: