[swift-evolution] [Review] SE-0194: Derived Collection of Enum Cases

Hooman Mehr hooman at mac.com
Sun Jan 14 15:15:59 CST 2018


A strong +1 on this approach instead of the current revision of SE-0194.

This is a very focused solution to a very focused need. It sidesteps the issues of a protocol based approach (potential for abuse or deciding/defining the intended uses of such protocol). Also, we already have the underpinnings of such literals, which makes it straightforward to implement. 

If we decide to do anything other than this, it will need a pretty strong argument.

This is also the least disrupting solution as it is basically what we are manually doing right now: We declare a static property that is nothing more than an array literal that captures cases that exist at the time of compilation in the source code order. We can just replace the manually written array literal with this one and be sure it will stay in sync, which minimizes overhead of transition for existing code and does not impose any particular style or arise the questions about the data type of this “collection”. (We can use any type that is `ExpressibleByArrayLiteral`)

We could also have a variant of this literal that would also capture the related metadata. For example, each element of the literal array could be tuples of each case value and another literal (an enum case maybe) that represents the availability metadata for that case.

Hooman

> On Jan 14, 2018, at 8:16 AM, Ben Rimmington via swift-evolution <swift-evolution at swift.org> wrote:
> 
> An alternative is a special #knownCases(of:) literal.
> 
> Its value is an array literal of the enum cases known at compile time.
> 
> This could also work with enums imported from Objective-C.
> 
> -- Ben
> 
>> On 10 Jan 2018, at 22:54, Jordan Rose wrote:
>> 
>> [Proposal: https://github.com/apple/swift-evolution/blob/master/proposals/0194-derived-collection-of-enum-cases.md <https://github.com/apple/swift-evolution/blob/master/proposals/0194-derived-collection-of-enum-cases.md>]
>> 
>> I think this is generally reasonable, and none of the names offend me enough to weigh in on that discussion. I do think it's a little weird that @objc enums defined in Swift cannot conform to ValueEnumerable, just because imported enums won't. (But especially while knee-deep in SE-0192, I think it's correct that imported enums won't. The exception could be C enums marked `enum_extensibility(closed)`, but I'm not convinced we need that yet.)
>> 
>> The biggest problem I have is unavailable cases. An unavailable case must not be instantiated—consider an enum where some cases are only available on iOS and not macOS. (I bet we optimize based on this, which makes it all the more important to get right.)
>> 
>> I think you should explicitly call out that the derived implementation only kicks in when ValueEnumerable is declared on the enum itself, not an extension. Or if that's not the case, it should be limited to extensions in the same module as the enum. (You could add "unless the enum is '@frozen'", but that's not really necessary.)
>> 
>> I don't think this should be implemented with a run-time function; compile-time code generation makes more sense to me. But that's an implementation detail; it doesn't change the language surface.
>> 
>> Jordan
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20180114/78c67d1f/attachment.html>


More information about the swift-evolution mailing list