[swift-evolution] ValueEnumerable protocol with derived implementation for enums

Howard Lovatt howard.lovatt at gmail.com
Sat Apr 16 18:01:11 CDT 2016

Re. OptionSetType

If you wrote:

    enum Ex { case one, two, three }

The compiler could do:

   - struct Ex : OptionSetType
   <http://swiftdoc.org/v2.2/protocol/OptionSetType> {
   -   private let weight: Int <http://swiftdoc.org/v2.2/type/Int>
   -   private init(_ weight: Int <http://swiftdoc.org/v2.2/type/Int>) {
   self.weight = weight }
   -   static let one = Ex(1)
   -   static let two = Ex(2)
   -   static let three = Ex(4)
   -   static let values: Ex = [one, two, three]
   - }

Plus the compiler would have to allow statics to have their type inferred,
i.e. you write .one and the compiler infers Ex.one.

This would allow set behaviour which I found very handy in Java.

PS Int only applicable for < 64 cases. Need BigInt for others.

On Saturday, 16 April 2016, Brent Royal-Gordon <brent at architechies.com>

> > I would suggest that ValuesEnumerable should expose allValues and the
> type of allValues should be a custom OptionSet implementation that iterates
> in declaration order.
> That would make sense if OptionSet were a generic type which took any
> integer-ish RawRepresentable (which I've advocated before, although I sort
> of understand why we haven't gone that route), but it isn't, so I don't
> think that makes a lot of sense for Swift.
> --
> Brent Royal-Gordon
> Architechies

-- Howard.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160417/88f7de3f/attachment.html>

More information about the swift-evolution mailing list