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

Joe Groff jgroff at apple.com
Tue Apr 11 11:38:01 CDT 2017


> On Apr 8, 2017, at 11:29 AM, Drew Crawford via swift-evolution <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
> 
> 

By design, Swift avoids making semantic rules based on that kind of analysis, since it would be brittle and difficult to describe when the compiler can and can't see that a condition holds nonlocally like this.

-Joe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170411/4b2469e0/attachment.html>


More information about the swift-evolution mailing list