1

I would like to constrain a generic type in Swift by the choice of different enum cases.

Below, a code example:

enum Test {
  case T1
  case T2
}

enum T1 {
  case one
  case two
}

enum T2 {
  case three
  case four
}


indirect enum Foo<T> {
  case empty
  case cons(T, Foo<T>)
}

let x: Foo<T1> = .cons(.one, .empty)

I have a first Enum which contains two cases with T1 and T2. I'm trying to constrain the type T in Foo Enum to have a collection of types available, which are T1 and T2 in my example. When I declare x, the allowed types are T1or T2 contain in Test, not another type. Hence, only types in Enum Test must be allowed.

My question: Is it possible to use my Enum to constrain the generic type ? If yes, how ? Otherwise, how can I do ?

4
  • To restate what I think you're asking... is it possible to have a generic type that could be either types A or B or C, but not others. And the answer is no. You can constrain to a protocol or class, or based on associated types of a protocol
    – New Dev
    Jul 10, 2020 at 14:15
  • Thanks for your answers, you understood my question. Can I do this kind of things with Protocols ?
    – damdamo
    Jul 10, 2020 at 14:29
  • What do you mean "this kind of things"? If you mean, can you constrain to a protocol, then yes - that's exactly what I said in my comment. Maybe I misunderstand what you're asking...
    – New Dev
    Jul 10, 2020 at 14:43
  • Sorry, I had to be more precise. I wanted to know if I can constrain how I would like (so T1 and T2) with protocols. Imagine that I can have 10 different types to constrain, how should I do ?
    – damdamo
    Jul 10, 2020 at 14:59

1 Answer 1

1

Method 1

According to the comments, I think this is what you are trying to accomplish:

protocol MyEnums {}

enum T1: MyEnums { case one, two }
enum T2: MyEnums { case one, two }
enum T3 { case one, two }

let foo: [MyEnums] = [T1.one, T1.two, T2.one, T2.two]

The array can only contain objects conforming to the MyEnums protocol.
Note how enum T3 cannot be stored within foo because it does not conform to the protocol MyEnums.


Method 2

Here's a little more. You actually can do what you were asking:

protocol MyEnums {}
enum T1: MyEnums { case one, two }
enum T2: MyEnums { case one, two }
enum T3 { case one, two }
indirect enum Foo<T: MyEnums> {
  case empty
  case cons(T, Foo<T>)
}

Note that the generic is <T: MyEnums>.
This code will do exactly what you want. It can store enums conforming to the MyEnums protocol. So you can store enums T1 and T2 if they are conformed to MyEnums, but not T3 because it is not.

So you will be able to do this:

let x: Foo = .cons(T1.one, .cons(T1.two, .empty))

However this second method does not allow for this:

let x: Foo = .cons(T1.one, .cons(T2.one, .empty))

I'll try to see if we can do this..


Method 3

I got it!! This is exactly what you were asking for:

protocol MyEnums {}
protocol MyEnums1: MyEnums {}

enum T1: MyEnums1 { case one, two }
enum T2: MyEnums1 { case one, two }
enum T3 { case one, two }

indirect enum Foo<T: MyEnums> {
  case empty
  case cons(MyEnums1, Foo<T>)
}

let x: Foo<T1> = .cons(T1.one, .cons(T2.one, .empty))
print(x)

It required 2 protocols, but I finally fixed Method 2. Remember, you can only put in objects conforming to MyEnums1, so T3 is not an option. Perfect!

This was a very cool question to answer.

1
  • 1
    Thanks for the answer ! That's exactly what I wanted !
    – damdamo
    Jul 13, 2020 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.