[swift-evolution] Handling unknown cases in enums [RE: SE-0192]

Jordan Rose jordan_rose at apple.com
Wed Jan 10 14:06:31 CST 2018



> On Jan 10, 2018, at 12:04, Jose Cheyo Jimenez via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> 
> On Tue, Jan 9, 2018 at 10:10 PM, Chris Lattner <clattner at nondot.org <mailto:clattner at nondot.org>> wrote:
> On Jan 9, 2018, at 3:07 PM, Jose Cheyo Jimenez <cheyo at masters3d.com <mailto:cheyo at masters3d.com>> wrote:
> > Hi Chris,
> >
> > This is great. Thanks for spending time on this! I am in favor of `case #unknown` to only match unknown cases.
> >
> > 1) Would #uknown be available to RawRepresentable structs?
> 
> I haven’t considered this, so I’m not sure how that would work.  I had assumed that this would be an enum specific concept.
> 
> What do you have in mind?  If your raw representation is itself an enum, then of course this would work.
> 
> I was just curious since a RawRepresentable structs ( and OptionSet ) are very similar to non exhaustive enums in that they would require handling unknown cases. Currently RawRepresentable is required to use a default even if all cases are accounted for. 
> 
> struct Directions: OptionSet {
>     let rawValue: UInt8
>     static let up    = Directions(rawValue: 1 << 0)
> }
> 
> let myDir = Directions.up
> 
> switch myDir
> {
>     case .up : print("matched")
>     default: print("default required") 
> }

Since an option set has 2^N possible values, not just N, we don't expect anyone to be switching over them! And it wouldn't be correct to try to do so exhaustively anyway.

Jordan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20180110/9adcb5da/attachment.html>


More information about the swift-evolution mailing list