[swift-evolution] Warning when omitting default case for imported enums
Charlie Monroe
charlie at charliemonroe.net
Tue Feb 7 15:11:58 CST 2017
> On Feb 7, 2017, at 7:54 PM, Tanner Nelson <tanner at qutheory.io> wrote:
>
> You'd still get the compiler helping you with new cases when working with enums within your module.
That's my point. My projects usually consist of several modules, as I've noted and I don't need to extend the enum from the other module (and in 99% of the cases it wouldn't make sense).
And you'd get no error in the other modules, which is terrible if your default label goes to fatalError and you need to catch all these cases at runtime.
Example:
My project consists of Core framework that is shared among all my projects, then the app has AppCore that uses the Core framework and is shared between macOS and iOS apps and then I have the actual app.
I.e. 3 modules that make one app. No need to extend enums. None so far. With what you suggest, I define an enum in the Core framework, AppCore and the App modules need to add "default" clause to their switch statements, usually fatalError, since that's the only plausible thing to do.
Adding another member to the enum (in Core) does not create errors/warnings in AppCore and App, so the new enum case is handled by fatalError, causing the app to crash at runtime.
> There's less use in having the compiler help you find new cases when they can only be added between major versions of a package. The benefit to cost ratio here is imbalanced. Enum cases are forever frozen from version x.0.0 of a package forward (until the next major bump). No other programming language I know of prevents adding enum cases in a minor release. This makes using enums in public API (which are incredibly useful) very difficult to do while following semver.
Not really. I have many in-house frameworks that can simply add enum values - since it's all within my projects, I don't need to worry about breaking 3rd party code.
> Could you explain more what you mean by public vs open enums?
Now you have two ways to offer class API to another module:
- declare it public - to the other module, it appears as "final".
- declare it open - in the other module, it can be subclassed and members declared as open can be overridden
In the same sense, you would declare an enum "open" instead of "public" to allow new members to be declared.
>
> Best,
> Tanner
>
> Sent from my iPhone
>
> On Feb 7, 2017, at 16:21, Charlie Monroe <charlie at charliemonroe.net <mailto:charlie at charliemonroe.net>> wrote:
>
>> -1
>>
>> Not having the default case allows you to rely on the compiler to handle new options once they are added. Most of my apps consist nowadays from multiple modules and this would be massively inconvenient.
>>
>> The possible future features may be non-breaking if we consider that we will not allow enums to be extended by default, but would need to be marked explicitely as extendable. Similar to public vs. open classes.
>>
>>> On Feb 7, 2017, at 4:12 PM, Tanner Nelson via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>
>>> Hello Swift Evolution,
>>>
>>> I'd like to propose that a warning be emitted when default cases are omitted for enums from other modules.
>>>
>>> What this would look like:
>>>
>>> OtherModule:
>>> ```
>>> public enum SomeEnum {
>>> case one
>>> case two
>>> }
>>>
>>> public let global: SomeEnum = .one
>>> ```
>>>
>>> executable:
>>> ```
>>> import OtherModule
>>>
>>> switch OtherModule.global {
>>> case .one: break
>>> case .two: break
>>> ^~~~~ ⚠︎ Warning: Default case recommended for imported enums. Fix-it: Add `default: break`
>>> }
>>> ```
>>>
>>> Why:
>>>
>>> Allowing the omission of a default case in an exhaustive switch makes the addition of a new case to the enum a breaking change.
>>> In other words, if you're exhaustively switching on an enum from an imported library, the imported library can break your code by adding a new case to that enum (which the library authors may erroneously view as an additive/minor-bump change).
>>>
>>> Background:
>>>
>>> As a maintainer of a Swift framework, public enums have been a pain point in maintaining semver. They've made it difficult to implement additive features and have necessitated the avoidance of enums in our future public API plans.
>>>
>>> Related Twitter thread: https://twitter.com/tanner0101/status/796860273760104454 <https://twitter.com/tanner0101/status/796860273760104454>
>>>
>>> Looking forward to hearing your thoughts.
>>>
>>> Best,
>>> Tanner
>>>
>>> Tanner Nelson
>>> Vapor
>>> +1 (435) 773-2831
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> 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/20170207/ac5df35f/attachment.html>
More information about the swift-evolution
mailing list