1

Optional in Swift allows for magic where switch-ing over an optional enum flattens the cases into a single switch statement.

Given:

enum Foo {
    case bar, baz
}

You can:

let foo: Foo? = .bar
switch foo {
case .bar:
    break
case .baz:
    break
case nil:
    break

Here, Optional is an enum and Foo is an enum, but just one single statement is enough to cover all cases of both.

Question: can I declare my own enum inside another enum so that the cases can be handled in a flat way too?

So that I could:

enum Foo<Bar> {
    case nope
    case dope(Bar)
}

enum Baz {
    case yep
}

let b: Foo<Baz> = .dope(.yep)

switch b {
case .nope:
    break
case .yep:
    break
}

Maybe if I call the case Foo.dope as Foo.some? Maybe there is an annotation that I can use?

1
  • I wonder if actually it works when you're not using generics... Oct 23, 2021 at 14:02

1 Answer 1

2

You could do this with …

switch b {
case .nope: // stuff
case .dope(.yep): // other stuff
case .dope(.someOtherCase): // more stuff
}

Responding to your comment…

Imagine you could flatten it like you said. How would you deal with…

enum Foo {
    case a
    case b(Bar)
    case c(Bar)
}

If you are allowed to exclude the .b from the switch then there is no way to differentiate between .b and .c.

15
  • Yeah, I mean, okay, yeah, butttttt---- I wonder if I can get rid of .dope inside the switch entirely... :) Oct 23, 2021 at 14:01
  • 1
    I managed to get as far with my own enum as allowing it to use nil and a wrapped value directly in the switch. But it still required me to add .no for the switch to be exhaustive. I pretty much created my own Foo enum with all the code from the Optional link I sent earlier. I couldn’t find anything about how they managing to remove the .none requirement from the switch when using nil. I’d still argue that what you’re trying is probably too much for too little benefit when you can still “flatten” the switch by using .dope(.yes) as the pattern.
    – Fogmeister
    Oct 24, 2021 at 10:24
  • 1
    Damnit, now I'm interested in how this works myself. LOL! I created a gist to show where I got to... gist.github.com/oliverfoggin/697e9340a5705800adf6ec5ae4cac29d but couldn't get some of the features to work. I've added a swift forums question forums.swift.org/t/…
    – Fogmeister
    Oct 24, 2021 at 19:09
  • 2
    Ok… this is from an ex apple engineer… twitter.com/txaiwieser/status/1452379958219788299?s=20 The way that Optional does all this fancy stuff is that they have baked a lot of it into the compiler so that the compiler gives it these “magic powers” that you referred to. So, as for writing your own, you definitely won’t be able to replicate everything yourself.
    – Fogmeister
    Oct 24, 2021 at 21:05
  • 1
    No worries man. It’s all good 😊 I actually got really interested in how it works. 😂
    – Fogmeister
    Oct 25, 2021 at 11:33

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.