[swift-evolution] [Idea] Find alternatives to `switch self`

Brent Royal-Gordon brent at architechies.com
Wed Mar 23 19:54:15 CDT 2016

> Type-switching is a “feature” of enums. If you don't want it, you could
> consider using protocols instead :-)

I don't think it's a bad thing that you *can* switch on an enum's type. Rather, I think that it is so commonly necessary that we should consider ways to avoid having to state it explicitly.

To try to get an idea of how common this is, I did a quick survey of the standard library. Of the 35 enum instance members I found:

* 12 had a `switch self` statement at the top level.
* 12 more, mainly in HashedCollections.swift.gyb, had a stdlib-only speed hack followed by a `switch self` containing the rest of the code. In normal user code, these would have just had the `switch self`.
* 11 had other code at the top level.

2/3 of members being entirely inside a `switch self` fits with my general sense of how things go down in user code.

I feel like, at that point, the `switch self` is basically just boilerplate, and we ought to consider whether or not we can get rid of it somehow.

Brent Royal-Gordon

More information about the swift-evolution mailing list