LuisPinto
LuisPinto

Reputation: 1697

How can I create a enum from another enum in Swift?

Is there a way having a enum like

enum Foo: Int {
   case unknown
   case secondCase
   case thirdCase
}

To create another enum to contain only some specific cases of that enum?

Something like

enum Bar: Int {
   case Foo.unknown
   case Foo.secondCase
}

Thank you in advance

Upvotes: 1

Views: 579

Answers (1)

Sam
Sam

Reputation: 2456

To the best of my knowledge, no, but you can do this:

enum Bar: Int {
    case unknown
    case secondCase
}

enum Foo: Int {
    case bar(Bar)
    case thirdCase
}

You'd have to implement your own Equatable, Hashable, etc., conformance, but it could be done. Enums are not designed for it, though, and neither is the language.

In the future, someone could propose this on the Swift Forums, where the community makes proposals to evolve the language.

Upvotes: 3

Related Questions