Where I can read about motivation of use .none while switching optional enumerations

Hello. I discovered that if we are switching optional enumeration we have case .none.

enum Temperature {
    case cold, warm
}

func checkTemp(temperature: Temperature?) {
    switch temperature {
    case .cold: break
    case .warm: break
    case .none: break
    }
}

Please give me the link to proposal of that feature.

1 Like

There are many layers to unravel here

  1. all these things were there before swift was even open source, so there are no proposals for these
  2. Temperature? is a short way to write Optional<Temperature>
  3. Optional is an enum with two cases, some and none https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift#L122
  4. when you have a static member Foo.bar that returns Foo and you're in a context that expect instance of Foo you can just write .bar (for example let x: Int = .zero )
  5. if you have value of type Foo you can use it in places that expect Foo? without explicitly writing .some (for example let x: Int? = 123 )

What you're doing is switching on two enumerations, one nested inside another. We can write it more explicitly to make it more clear where none is coming from

func checkTemp(temperature: Optional<Temperature>) {
    switch temperature {
    case Optional.some(Temperature.cold): break
    case Optional.some(Temperature.warm): break
    case Optional.none: break
    }
}
9 Likes