[swift-evolution] [Pitch] Can we make `default` on switches optional?

Adrian Zubarev adrian.zubarev at devandartist.com
Mon Oct 3 05:14:20 CDT 2016

I know that there is this note in Commonly Rejected Changes:

Remove support for default: in switch and just use case _:: default is widely used, case _ is too magical, and default is widely precedented in many C family languages.
I really like to use the switch instead of if case for pattern matching, just because it’s neat block design. I do not want to remove default from switches because it’s a must have and powerful feature.

I’d like to know why switches must be exhaustive.

switch someValue {
case …:
    // Do something
case …:
    // Do something else

    () // useless nop; do nothing when no pattern matched

// VS:

if case … {
} else if case … {
} else if case … {
} // No need for `else`
Can’t we make default optional, or at least on non-enum values?

Adrian Zubarev
Sent with Airmail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161003/98cb82f9/attachment.html>

More information about the swift-evolution mailing list