[swift-evolution] Proposal: Enum 'count' functionality

Erica Sadun erica at ericasadun.com
Mon Dec 21 14:24:20 CST 2015


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 }

:)

-- E


> On Dec 21, 2015, at 1:14 PM, Joe Groff <jgroff at apple.com> wrote:
> 
> 
>> On Dec 21, 2015, at 12:09 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> My wild aspirations in a nutshell:
>> 
>> Core enums:  Any enum that's created without raw or associated values, e.g. enum MyEnum {case This, That, Whatever, Etc},  can (and should) be Array<Self> representable. This would add intrinsic ordering and raw value construction starting with 0, up to count - 1. End-devs could use the ordering or not use the ordering, but it would be possible to convert to bit representation (1 << this.rawValue),  support iteration through the enumeration, introduce ranges for switches, etc. A massive improvement.
>> 
>> Raw value enums: Any enum that uses raw values, e.g. enum ForExample: String {case Hello = "hello", There = "there"} should be representable as Set<T>
>> 
>> Associated type enums: all bets are off
> 
> There's only one kind of enum fundamentally, and I think we should make for more continuity between different instances of enum rather than less. Any type with a reasonably finite number of states ought to be able to support a `values` collection. If it happens to have a RawRepresentable conformance, mapping from values to rawValues is easy, and could be provided by a protocol extension on ValueEnumerable where Self: RawRepresentable. Enums with associated values that are themselves ValueEnumerable could enumerate all the values of the associated value, so something like this:
> 
> enum Foo: ValueEnumerable { case A, B, C }
> 
> enum Bar: ValueEnumerable { case X(Foo), Y(Foo) }
> 
> would give you `.X(.A)`, `.X(.B)`, `.X(.C)`, etc. (You could also derive ValueEnumerable for structs by walking the cartesian product of the stored properties' values, but I don't know if that's really useful.)
> 
> -Joe

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


More information about the swift-evolution mailing list