[swift-evolution] switch must be exhaustive, consider adding a default clause

Kevin Nattinger swift at nattinger.net
Mon Apr 10 11:53:47 CDT 2017


> On Apr 10, 2017, at 9:18 AM, Josh Parmenter via swift-evolution <swift-evolution at swift.org> wrote:
> 
> case .none isn’t handled.

It shouldn’t need to be handled in the second switch because it’s impossible. Not just developer thinks “impossible” but easily provable by static analysis the compiler is doing it anyway—I wouldn’t be surprised if including the case resulted in an “unreachable code” warning (in fact, it probably should).

> This should probably be on the swift-users list though, no?

File a bug, IMO. 

> Best,
> Josh
> 
> 
> 
> On Apr 8, 2017, at 11:29 AM, Drew Crawford via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org><mailto:swift-evolution at swift.org <mailto:swift-evolution at swift.org>>> wrote:
> 
> 
> 
> Is there a good reason we do not compile this:
> 
> import UIKit
> 
> func foo(operation: UINavigationControllerOperation) {
>    switch(operation) {
>    case .push: /* snip */ break
>    case .pop: /* snip */ break
>    default:
>        preconditionFailure("This is a silly operation")
>    }
>    switch(operation) {
>        case .push: /* snip */ break
>        case .pop: /* snip */ break
>         //error: Switch must be exhaustive, consider adding a default clause
>    }
> }
> 
> The switch *is* exhaustive, because the default case is unreachable.  The compiler could infer as much from branch analysis.
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org><mailto: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>
> 
> 
> Joshua Parmenter | Engineering Lead, Apple Technologies
> 
> T 248 777 7777
> C 206 437 1551
> F 248 616 1980
> www.vectorform.com <http://www.vectorform.com/><http://www.vectorform.com/ <http://www.vectorform.com/>>
> 
> Vectorform
> 2107 Elliott Ave Suite 303
> Seattle, WA  98121 USA
> 
> Think Tank. Lab. Studio.
> We invent digital products and experiences.
> 
> SEATTLE | DETROIT | NEW YORK | MUNICH | HYDERABAD
> _______________________________________________
> 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/20170410/ee8451e5/attachment.html>


More information about the swift-evolution mailing list