[swift-evolution] [Pitch] Can we make `default` on switches optional?
Jeremy Pereira
jeremy.j.pereira at googlemail.com
Mon Oct 3 06:39:42 CDT 2016
> On 3 Oct 2016, at 11:14, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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
>
> default:
> () // 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?
>
>
I absolutely cannot see why
default: break
doesn’t already do this for you. It’s not a useless no-op, it signifies to the compiler that you really do want to ignore the other cases and it’s not a mistake.
I think there is a case for allowing default: break to be at the top of the switch but other than that, I like the fact that not having a an explicitly exhaustive switch generates an error.
>
>
> --
> Adrian Zubarev
> Sent with Airmail
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list