[swift-evolution] [swift-dev] Re-pitch: Deriving collections of enum cases

Robert Widmann devteam.codafi at gmail.com
Fri Nov 10 15:46:41 CST 2017


> On Nov 6, 2017, at 2:54 AM, Jacob Bandes-Storch via swift-dev <swift-dev at swift.org> wrote:
> 
> Over a year ago, we discussed adding a magic "allValues"/"allCases" static property on enums with a compiler-derived implementation. The original proposal PR <https://github.com/apple/swift-evolution/pull/114> has been reopened for Swift 5 after languishing for a while, and I'd like to revisit it and make some changes before it goes up for formal review.
> 
> Prior discussion: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160411/015098.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160411/015098.html> (good luck finding the rest of the thread if you weren't on the list at the time...)
> 
> [cc'd swift-dev for importer/availability-related topics below.]
> 
> **Naming**
> 
> Given the complexity gap between a simple enumeration of cases and full support for non-enum types and associated values (which we don't intend to support with this proposal), I think it might be a good idea to adopt the names CaseEnumerable/allCases instead of ValueEnumerable/allValues.
> 
> The original proposal didn't expose allValues as a requirement, for fear of unduly restricting its type. However, if the protocol's scope is more limited, static var allCases can be exposed as a requirement since the implementations are not likely to be complex. Furthermore...
> 
> 
> **Generics**
> 
> Since SE-0142 <https://github.com/apple/swift-evolution/blob/master/proposals/0142-associated-types-constraints.md> was implemented in Swift 4, we now have more expressive options for the protocol requirements:
> 
>   // 1 - array only
>   protocol CaseEnumerable {
>     static var allCases: [Self] { get }
>   }
> 
>   // 2 - any sequence
>   protocol CaseEnumerable {
>     associatedtype CaseSequence: Sequence where CaseSequence.Element == Self
>     static var allCases: CaseSequence { get }
>   }
> 
>   // 3 - any collection
>   protocol CaseEnumerable {
>     associatedtype CaseCollection: Collection where CaseCollection.Element == Self
>     static var allCases: CaseCollection { get }
>   }
> 
> This restricts the CaseEnumerable protocol to be used as a generic constraint, but that'd be true even with a plain array because of the Self type.
> 
> Personally I like the flexibility provided by the associatedtype, but I also recognize it won't be incredibly useful for enums — more so if we wanted to provide e.g. UInt8.allValues, whose ideal implementation might be "return 0...UInt8.max". So I could see allowing allValues to be any sequence or collection, but flexibility for allCases might be less important. Others should weigh in here.

Generalizing the result type is probably a good thing to do before we finalize the user-facing component of this.  I wrote the interface with an Array at the time because it was easy and because the first implementation of this tried to directly synthesize an array literal expression.

> 
> 
> **Implementation strategy and edge cases**
> 
> Last year <https://twitter.com/CodaFi_/status/920132464001024001>, Robert Widmann put together an implementation of CaseEnumerable: https://github.com/apple/swift/compare/master...CodaFi:ace-attorney <https://github.com/apple/swift/compare/master...CodaFi:ace-attorney> 
> I'd love to hear from anyone more familiar with the code whether there's anything we'd want to change about this approach.
> 
> A few tricky situations have been brought to my attention:
> 
> - Enums imported from C/Obj-C headers. Doug Gregor writes: "The autogenerated allValues would only be able to list the enum cases it knows about from the header it was compiled with. If the library changes to add cases in the future (which, for example, Apple frameworks tend to do), those wouldn’t be captured in allValues."
> 
> My understanding of the runtime/importer is very shallow, but with the current metadata-based strategy, I suspect imported enums couldn't be supported at all, or if they could, the metadata would be generated at import time rather than loaded dynamically from the library, which naturally wouldn't behave the same way when you drop in an upgraded version of the library. Is that correct?

Exactly.  Today, we don’t write out metadata for foreign enumerations and it would be interesting to see how that would look if we decide to do so in the future.  Either way, leaving it up to the authors of Objective-C/C to create a Swift wrapper to get access to this protocol is a little unfortunate, but not that big a deal ultimately.

> 
> - Enums with availability annotations on some cases. Doug Gregor writes: "if I have a case that’s only available on macOS 10.12 and newer, it probably shouldn’t show up if I use allValues when running on macOS 10.11."
> 
> If we fetch cases from the enum metadata, does this "just work" since the metadata will be coming from whichever version of the library is loaded at runtime? If not, is it at least possible to extract availability info from the metadata? Finally, if not, should we try to synthesize an implementation that uses #available checks, or just refuse to synthesize allCases?

Up front, I want to point out that using a metadata-based approach will still enumerate unavailable cases.

enum Foo {
  case bar
  case baz
  @available(*, unavailable)
  case quux
}

print(unsafeBitCast(Int8(2), to: Foo.self))

Whether or not this is desirable is a point that will need to be fleshed out.  My original implementation did take conditional availability into account and tried to group together similarly available cases - but (modulo how weird that was) that implementation also assumed that “allCases” meant that the resulting collection excluded unavailable cases.  I don’t lean one way or the other on this issue but I would like it settled in the proposal.  On the one hand, it will now be possible to synthesize unavailable enum cases, on the other things like switches over enums with unavailable cases must still handle the unavailable case.

> 
> - Should it be possible to add a CaseEnumerable conformance in an extension? My thinking is: we want to make sure the metadata is coming from the module that defines the enum, so we could restrict autogeneration of allCases to that same module. (That is, it wouldn't be possible to synthesize allCases for a CaseEnumerable extension on an enum from another module.) Although, it may be that I am missing something and this restriction isn't actually necessary. The question to answer is: in exactly which circumstances can the implementation be synthesized?

The metadata is there across framework boundaries.  

From an aesthetic POV, I’m partial to Slava’s idea that it shouldn’t ever be synthesized (because I’m lazy, go figure) which makes the protocol opt-in.  But this also means users may have to go through multiple rounds of “why doesn't X framework/overlay export CaseEnumerable implementations” with authors.  I think a good middle ground is to make conformance opt-in then teach Mirror to recognize when it’s reflecting a metatype for a simple enumeration to provide a getter for its cases.

~Robert Widmann

> 
> 
> Looking forward to hearing everyone's thoughts,
> Jacob
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

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


More information about the swift-evolution mailing list