[swift-evolution] Consolidate Code for Each Case in Enum
David Waite
david at alkaline-solutions.com
Mon Jan 9 15:38:37 CST 2017
> Enum cases _feel_ like separately defined, but tightly related structs because each case can have distinct associated values. They have special privileges that a family of structs doesn't have, like `self = .otherCase`. Enums are really awesome.
<musings>
I think this is the primary difference between a “tagged union” and a Swift enum - I might have a union of several discrete types in other languages, but in swift it is actually an enumeration of cases within a single type. The individual cases are not types.
This was actually one of the reasons that drove case labels and static values to stylistically change to lowercase in Swift 3 - to avoid building a misconception of enum cases being distinct types.
I’ve wanted this feature in the past as well, but wonder if at some point encouraging support for a distinct union of types makes more sense than expanding enum - say, a closed protocol only allowing implementation by specific types declared within its module.
-DW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170109/1797050a/attachment.html>
More information about the swift-evolution
mailing list