[swift-evolution] Switch on an optional enum -- `case: nil`?

David Sweeris davesweeris at mac.com
Thu May 5 10:56:29 CDT 2016


Oh, cool! I didn’t realize this syntax existed.

On May 5, 2016, at 10:52 AM, Krzysztof Siejkowski via swift-evolution <swift-evolution at swift.org> wrote:
> 
> It doesn’t compile because you’re switching on the Optional and trying to pattern match it to Coin.
> 
> It compiles right when you pattern match it to Optional<Coin>:
> 
> switch result {
> case .heads?: print("heads")
> case .tails?: print("tails")
> case nil: print("not yet flipped")
> }
> 
> Best,
> Krzysztof
> 
> On 5 May 2016 at 17:42:24, Eric Miller via swift-evolution (swift-evolution at swift.org <mailto:swift-evolution at swift.org>) wrote:
> 
>> Wondering if you guys think this is a reasonable idea.
>> 
>> I was switching on an optional enum today, and naturally gravitated towards including nil as a case:
>> 
>> enum Coin {
>>   case heads
>>   case tails
>> }
>> var result: Coin?
>> 
>> switch result {
>> case heads: print("heads")
>> case tails: print("tails")
>> case nil: print("not yet flipped") // exhaustive
>> }
>> 
>> Doesn't compile, and of course I can if-let or guard to unwrap instead, but I like the compactness of this.
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160505/a291718c/attachment.html>


More information about the swift-evolution mailing list