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
cukr
2
There are many layers to unravel here
- all these things were there before swift was even open source, so there are no proposals for these
-
Temperature? is a short way to write Optional<Temperature>
- Optional is an enum with two cases,
some and none swift/Optional.swift at main · apple/swift · GitHub
- 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 )
- 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