[swift-evolution] Pre-proposal: CaseEnumerable protocol (derived collection of enum cases)

Tino Heth 2th at gmx.de
Thu Jan 21 07:37:24 CST 2016


Attributes would be an option as well — but even if they are imho the natural choice, I think I would prefer a "protocol with synthesized default behavior".

>> Introduce a CaseEnumerable protocol. Conforming to CaseEnumerable will automagically derive a static var cases, whose type is a CollectionType of all the enum's values.
> 
> Hm, I don’t think I like that. Are there other examples of magic protocols in Swift?


Same opinion — if a protocol is the way to go, it shouldn't receive more special treatment than necessary:
Every type should be allowed to conform to the protocol, and enums could work like Joe suggests
> This is just my opinion, but I think declaring a conformance ought to be enough. You could imagine the compiler-synthesized implementation being provided by a protocol extension with sufficiently advanced language features from the future to implement the default behavior.

There might be even situations where it could make sense to have "cases" that are generated at runtime (at least a can't see a reason to forbid it).

Tino
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160121/fb757fc9/attachment.html>


More information about the swift-evolution mailing list