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

Al Skipp al_skipp at fastmail.fm
Fri Dec 11 08:13:44 CST 2015

> You can already do something like this as well. Sure it's a bit verbose, but for the cases where you need to ignore the associated value you can do a one-to-one mapping. You can then simply do a switch(enumVar.type()) { ... }.
> enum EnumType {
>     enum EnumTypeType {
>         case TypeA
>         case TypeB
>         case TypeC
>         case TypeD
>     }
>     case TypeA(String)
>     case TypeB(Bool)
>     case TypeC()
>     case TypeD(String)
>     func type() -> EnumTypeType {
>         switch(self) {
>         case .TypeA(_):
>             return .TypeA
>         case .TypeB(_):
>             return .TypeB
>         case .TypeC():
>             return .TypeC
>         case TypeD(_):
>             return .TypeD
>         }
>     }
> }

I initially considered this option too, but it doesn’t address the question:

> 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.

The request was the ability to create enums without supplying the associated parameters, the above implementation doesn’t allow that (it’s simply not feasible). Also, it’s possible to pattern match an enum without binding it’s associated values, so there’s no need to create an internal ‘shadow’ enum without the parameters.

A very ugly approach would be to make the associated values Optionals, but that’s quite a hacky thing to do for the use case.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/0d2187b5/attachment.html>

More information about the swift-evolution mailing list