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

Paul Cantrell cantrell at pobox.com
Wed Jan 10 13:24:15 CST 2018



> On Jan 10, 2018, at 12:44 PM, Karl Wagner <razielim at gmail.com> wrote:
> 
> 
> 
>> On 10. Jan 2018, at 17:22, Paul Cantrell via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>>> What is your evaluation of the proposal?
>> 
>> +1. Yes please. Long overdue.
>> 
>>> Is the problem being addressed significant enough to warrant a change to Swift?
>> 
>> It’s a long-standing sore thumb. The proposal’s evidence of community demand fits my own experience: I’ve wanted this on multiple occasions.
>> 
>>> Does this proposal fit well with the feel and direction of Swift?
>> 
>> Yes, and in particular, on the name bikeshedding:
>> 
>> I favor property names with the “all” prefix, whether allValues or allCases. Looking over my own code, I’ve almost always used the word “all” for this when I had to hand-roll it — and either allValues or allCases make reasonable sense in my code when I substitute them.
>> 
>> Whichever protocol name we choose, the property name should be consistent:
>> 
>> 	ValueEnumerable → allValues
>> 	CaseEnumerable → allCases
>> 
>> Either ValueEnumerable or CaseEnumerable would be a fine name. Contra Chris, I slightly prefer ValueEnumerable, because it extends to situations where we still want to enumerate a fixed set of possibilities which don’t strictly correspond to enum cases but still have that sort of flavor. For example, one might want:
>> 
>>     enum SideOfBody
>>       {
>>       case left
>>       case right
>>       }
>> 
>>     enum Limb: ValueEnumerable
>>       {
>>       case arm(SideOfBody)
>>       case leg(SideOfBody)
>> 
>>       static let allValues =
>>         [
>>         arm(.left),
>>         arm(.right),
>>         leg(.left),
>>         leg(.right)
>>         ]
>>       }
>> 
>> To my eyes, this code reads better than it would with CaseEnumerable / allCases.
>> 
>>> If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
>> 
>> Java’s enums had this from the beginning, and Josh Bloch’s design for that feature has always worked nicely. Java’s design is slightly different: `Foo.values()` returns Foo[]. However, Swift doesn’t need to follow either that name or type choice: (1) Java doesn’t use the term “case” as Swift does, (2) the “all” prefix better fits Swift’s API guidelines IMO, and (3) using a concrete array type has as opposed to Collection has different implications in Java than it does Swift.
>> 
>> I _do_ agree  that the proposal should consider constraining the Collection to be Int-indexed. Why should it ever be otherwise? What’s the motivation for leaving that open?
> 
> 
> I don’t agree that the Collection should be Int-indexed. Source-order is not a very strong guarantee IMO, and it wouldn’t be good if people started writing things like "MyEnum.allValues[3]” to reference a specific case.

You do make a good point about the hidden brittleness of MyEnum.allValues[3].

Direct indexing of collections using arbitrary hard-coded int indexes is a way to introduce brittleness in many other existing contexts, and I’d say developers already know that it has a bad smell (or are making other far larger mistakes as well). However, using int indices for serialization seems a pitfall that would catch many people. Is there a way to make transient use of ints convenient (as in the proposal’s table view example) but persistent int values inconvenient? No.

There are clearly enums where source ordering is intentional and significant, and others where the enum is inherently unordered. An alternative would be two protocols, one where allValues is a Set and one where it is an Array, or something along those lines. That feels like overkill to me, however.

Perhaps the proposal as it stands is the best compromise: everything is ordered, even if the ordering is not stable between versions, but int indexing is inconvenient enough to turn away the more careless among us.

Cheers, P

> 
> If you know the specific case you are looking for, just write it directly. If you found an interesting case while iterating allValues, remember its (opaque) index and come back to it later.
> 
> I’m not a fan of Int-indexes in general. It’s practical to allow it for Array, but in general, for generic Collections, I think it implies an awful lot of knowledge about the Collection’s contents.
> 
> - Karl

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


More information about the swift-evolution mailing list