[swift-evolution] Proposal: Enum 'count' functionality
Joe Groff
jgroff at apple.com
Tue Dec 22 09:24:37 CST 2015
> On Dec 21, 2015, at 10:33 PM, Zef Houssney <zefmail at gmail.com> wrote:
>
> I agree with Stephen Celis that the best names for this (yet) are definitely `cases` and optionally `rawValues` if the cases are RawRepresentable.
>
> Regarding handling cases with associated values where some of those values are other enums, it seems odd to me to try to return every variation of each case. I can’t picture how that would work sanely when there are multiple associated values, such as a case like `MyCase(String, SomeEnum)`. Therefore I prefer the idea of having the array contain the constructor for cases with associated values. The main problem I see here is that the type system (as far as I know) can’t fully represent that type of thing, but I think it’s still potentially valuable. Related to this I’m putting together a separate proposal that explores a bit more on why I believe the enum constructor to be valuable, though that’s in a bit of a different context.
Generating a list of values for MyCase(String, SomeEnum) wouldn't make sense because there are an infinite number of Strings, so Strings don't make sense as a value-enumerable type. However, you could have a mostly-flat enum that uses subenums for organization:
enum Food {
enum Fruit {
case Apple, Banana, BellPepper
}
case fruit(Fruit)
enum Meat {
case Beef, Pork, Venison
}
case meat(Meat)
}
In many C family code bases it's common to group related enums by order, even if there's otherwise no intrinsic order among the elements. Preserving the structure of the cases in the type system makes them easier to understand and work with in pattern-matching, since you can handle all fruits by matching `.fruit(_)` instead of having to maintain `food >= FirstFruit && foo <= LastFruit` ranges. For these kinds of enums, recursively generating the associated values is still useful.
-Joe
> I shared these thoughts in a bit more detail here a couple weeks ago, with some followup in the next two messages:
>
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/001864.html
>
> Zef
>
>
>
>> On Dec 21, 2015, at 3:58 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> That would be okay too. Thank you, Santa Joe.
>>
>> -- E, who hopes she was on the nice list.
>>
>>> On Dec 21, 2015, at 3:56 PM, Joe Groff <jgroff at apple.com> wrote:
>>>
>>>
>>>> On Dec 21, 2015, at 12:24 PM, Erica Sadun <erica at ericasadun.com> wrote:
>>>>
>>>> I could be satisfied by such an approach. I could even be more satisfied if
>>>>
>>>>> enum Foo: ValueEnumerable { case A, B, C }
>>>>
>>>>
>>>> were also essentially an alias for
>>>>
>>>>> enum Foo: Int, ValueEnumerable { case A=0, B, C }
>>>>
>>>> :)
>>>
>>> If the value collection were sufficiently capable, you wouldn't necessarily need an implicit Int raw value; you could do `allValues.indexOf(.A)` to get the index for a case.
>>>
>>> -Joe
>>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
More information about the swift-evolution
mailing list