[swift-evolution] Request: Ability to refer to an enum case in abstract (without its associated value)

Evan Maloney emaloney at gilt.com
Thu Dec 10 22:53:37 CST 2015

>> I might be missing something, but you can do this already. 'case .CaseWithPayload:' matches any CaseWithPayload regardless of its payload.
>> -Joe

Hi Joe,

Thanks for the reply.

You're referring to the ability to test whether an existing enum value is of a given case regardless of the contents of its associated values. In other words, after a value has been created, you can test for whether it is of a specific case while ignoring any associated values.

I'm talking about the ability to create a value that represents the generic concept of an enum case that has associated values, but without actually specifying any associated values.

In Swift right now, you can programmatically represent an enum case that has associated values only by first supplying those associated values.

I think of it like the difference between a class and an instance. A class defines a type and it defines storage. An instance is of a certain type and puts specific values in the storage. An instance or value is a realization of a class or type. If I have an instance or value, I can ask it to tell me its type. I can store that type in a variable. There is also a notation to allow me to specify any such type as a literal.

Just as I can programmatically refer to the type of an value (or the class of an object instance), I'd like to be able to refer to a case in the abstract without also needing to supply associated values.  But that's not possible; an enum case that is declared with associated values can't exist in any form unless and until those values are specified.

Perhaps an example will help. Assume the following enum representing the universe of screens that can be displayed in an app:

enum Screens
    case Splash
    case Landing
    case UserPreferences(User)
    case StoreView(Store)
    case SaleView(Sale)
    case ProductView(Product)

Some of the cases have associated values, some do not. The ones that have associated values represent screens that require certain input in order to display content.

Now say I want to populate a debug menu that lets testers navigate anywhere in the app. I'd like a way to represent each screen in the app, but without any specific content.

I have an enum of Screens right here, but I can't actually use it for that purpose because I can't create values from it without supplying the associated values right then and there. So I can't add a "func allScreens() -> [Screens]" to the enum to allow me to query for this information.

The simplest work-around for this use case seems to be creating a parallel enum like:

enum ScreenTypes
    case Splash
    case Landing
    case UserPreferences
    case StoreView
    case SaleView
    case ProductView

    func allScreens() -> [ScreenTypes]

With such an enum, I could populate a debug menu.

Several times now when I've used enums that have associated values, I've wished for a parallel enum that had the same cases, but without associated values, so I could do things like that. I usually end of creating one, but then I lose the ability to have the compiler flag a whole class of errors.

Does my explanation make sense?

All the best,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151210/faeef86e/attachment.html>

More information about the swift-evolution mailing list