[swift-evolution] List of all Enum values (for simple enums)

David Waite david at alkaline-solutions.com
Wed Dec 9 12:19:28 CST 2015


There are a number of possible proposals for enhancements I see off the bat for “basic" enums (without associated data):

1. A generated implementation of SequenceType based on declaration order
2. A second interface or extension to the enum type could supply static max and min properties when the RawValue is Comparable
3. For enums with a String raw value, CustomStringConvertible and Streamable might make sense as well. You would likely want CustomDebugStringConvertible to also be defined to supply the original type-based value.

-DW

> On Dec 8, 2015, at 11:13 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> On Dec 8, 2015, at 10:09 PM, Jacob Bandes-Storch <jtbandes at gmail.com <mailto:jtbandes at gmail.com>> wrote:
>> 
>> Do you have any recommendations for where in the compiler this would happen, for those of us without thorough knowledge of all parts? What would the team need to see in a proposal for it to be sufficiently complete?
> 
> This would slot into the same logic that synthesizes memberwise initializers on structs.  That said, I think that hashing out the right user model and set of capabilities is the first place to start.
> 
> -Chris
> 
> 
>> On Tue, Dec 8, 2015 at 10:05 PM Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>> 
>> > On Dec 8, 2015, at 9:53 PM, Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> >
>> > Enums are often used without associated values:
>> >
>> >     enum Attribute { case Title, Date }
>> >     enum Style { case Dark, Light }
>> >
>> > In such cases, it's very useful to be able to iterate over all possible values. But you have to do it manually:
>> >
>> >     extension Attribute {
>> >         static var allValues: [Attribute] {
>> >             return [.Title, .Date]  // imagine this x100, and not autogenerated :(
>> >         }
>> >     }
>> >
>> > It would be nice if the compiler generated this for us. It doesn't have to be an Array, but any kind of SequenceType. Maybe something like "StaticCollection". Of course, this doesn't work in the general case, but I think simple enums are common enough that it might be worth doing.
>> 
>> This is a commonly requested feature, and tons of people would support the ability to do this somehow.  Similarly, for integer-backed enums, many people want to be able to know the “max” enum rawValue so they can index into them.
>> 
>> -Chris
>> 
> 
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151209/9b58c150/attachment.html>


More information about the swift-evolution mailing list