[swift-evolution] [Proposal] Allow enumerating cases in enumerations

Guillermo Peralta Scura gperaltascura at gmail.com
Sun Jul 3 21:06:51 CDT 2016


+1. The only thing that I don't like is that it may lead to confusion to
novice developers because it only work with "basic" enums i.e. enums
without associated values. The same thing happens with protocols with
associated types, where you can't use the protocol with associated types as
the type of properties, variables or constants like "basic" protocols.

El dom., 3 jul. 2016 a las 21:58, Saagar Jha via swift-evolution (<
swift-evolution at swift.org>) escribió:

> +1 since it makes it easy to get the number of possible enum values, but I
> thought I'd tell you: there's no "Wind" type in Pokémon. It's "Flying".
> On Sun, Jul 3, 2016 at 18:36 Gabriel Lanata via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>> Hello, this has been proposed multiple times before in different ways.
>>
>> Link to detailed proposal draft:
>>
>> https://github.com/gabriellanata/swift-evolution/blob/master/proposals/NNNN-allow-enumerating-cases-in-enumerations.md
>>
>> It is currently not possible to enumerate through all the possible cases
>> of an enumeration object without adding a hardcoded value or recurring to
>> unsafe workarounds. The proposal is to add a method of obtaining an array
>> or set of all the possible cases natively for enumerations without
>> associated values.
>>
>> The proposed solution is to implement a native `.cases` static var for
>> all enumerations without associated values. This would provide a strict
>> type-safe way of enumerating though the possible cases without risking
>> runtime errors. The resulting code is a lot cleaner and less prone to
>> errors caused by forgetting to update the hardcoded values when
>> modifying cases.
>>
>> Resulting code:
>>
>>    enum PokemonType {
>>        case Fire, Grass, Water, Wind
>>    }
>>    for pokemonType in PokemonType.cases {
>>        // Do stuff
>>    }
>>    PokemonType.cases.count // Returns 4
>>
>>
>> -------
>> Gabriel
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
> --
> -Saagar Jha
> _______________________________________________
> 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/20160704/d31bc8f6/attachment.html>


More information about the swift-evolution mailing list