Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-09 Thread Tony Allevato via swift-evolution
On Fri, Sep 8, 2017 at 5:14 PM Xiaodi Wu  wrote:

> On Fri, Sep 8, 2017 at 4:08 PM, Matthew Johnson via swift-evolution <
> swift-evolution@swift.org> wrote:
>
>>
>> On Sep 8, 2017, at 12:05 PM, Tony Allevato 
>> wrote:
>>
>>
>>
>> On Fri, Sep 8, 2017 at 9:44 AM Matthew Johnson 
>> wrote:
>>
>>> On Sep 8, 2017, at 11:32 AM, Tony Allevato 
>>> wrote:
>>>
>>>
>>>
>>> On Fri, Sep 8, 2017 at 8:35 AM Matthew Johnson 
>>> wrote:
>>>
 On Sep 8, 2017, at 9:53 AM, Tony Allevato via swift-evolution <
 swift-evolution@swift.org> wrote:

 Thanks for bringing this up, Logan! It's something I've been thinking
 about a lot lately after a conversation with some colleagues outside of
 this community. Some of my thoughts:

 AFAIK, there are two major use cases here: (1) you need the whole
 collection of cases, like in your example, and (2) you just need the number
 of cases. The latter seems to occur somewhat commonly when people want to
 use an enum to define the sections of, say, a UITableView. They just return
 the count from numberOfSections(in:) and then switch over the cases in
 their cell-providing methods.

 Because of #2, it would be nice to avoid instantiating the collection
 eagerly. (Also because of examples like Jonathan's, where the enum is
 large.) If all the user is ever really doing is iterating over them,
 there's no need to keep the entire collection in memory. This leads us to
 look at Sequence; we could use something like AnySequence to keep the
 current case as our state and a transition function to advance to the next
 one. If a user needs to instantiate the full array from that sequence they
 can do so, but they have to do it explicitly.

 The catch is that Sequence only provides `underestimatedCount`, rather
 than `count`. Calling the former would be an awkward API (why is it
 underestimated? we know how many cases there are). I suppose we could
 create a concrete wrapper for Sequence (PrecountedSequence?) that provides
 a `count` property to make that cleaner, and then have
 `underestimatedCount` return the same thing if users passed this thing into
 a generic operation constrained over Sequence. (The standard library
 already has support wrappers like EnumeratedSequence, so maybe this is
 appropriate.)

 Another question that would need to be answered is, how should the
 cases be ordered? Declaration order seems obvious and straightforward, but
 if you have a raw-value enum (say, integers), you could have the
 declaration order and the numeric order differ. Maybe that's not a problem.
 Tying the iteration order to declaration order also means that the behavior
 of a program could change simply by reördering the cases. Maybe that's not
 a big problem either, but it's something to call out.

 If I were designing this, I'd start with the following approach. First,
 add a new protocol to the standard library:

 ```
 public protocol ValueEnumerable {
   associatedtype AllValuesSequence: Sequence where
 AllValuesSequence.Iterator.Element == Self

   static var allValues: AllValuesSequence { get }
 }
 ```

 Then, for enums that declare conformance to that protocol, synthesize
 the body of `allValues` to return an appropriate sequence. If we imagine a
 model like AnySequence, then the "state" can be the current case, and the
 transition function can be a switch/case that returns it and advances to
 the next one (finally returning nil).

 There's an opportunity for optimization that may or may not be worth
 it: if the enum is RawRepresentable with RawValue == Int, AND all the raw
 values are in a contiguous range, AND declaration order is numerical order
 (assuming we kept that constraint), then the synthesized state machine can
 just be a simple integer incrementation and call to `init?(rawValue:)`.
 When all the cases have been generated, that will return nil on its own.

 So that covers enums without associated values. What about those with
 associated values? I would argue that the "number of cases" isn't something
 that's very useful here—if we consider that enum cases are really factory
 functions for concrete values of the type, then we shouldn't think about
 "what are all the cases of this enum" but "what are all the values of this
 type". (For enums without associated values, those are synonymous.)

 An enum with associated values can potentially have an infinite number
 of values. Here's one:

 ```
 enum BinaryTree {
   case subtree(left: BinaryTree, right: BinaryTree)
   case leaf
   case empty
 }
 ```

 Even without introducing an Element type 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-09 Thread Tony Allevato via swift-evolution
On Sat, Sep 9, 2017 at 11:36 AM Matthew Johnson via swift-evolution <
swift-evolution@swift.org> wrote:

>
>
> Sent from my iPad
>
> On Sep 9, 2017, at 11:42 AM, gs.  wrote:
>
> How does fragility play into this? Does this only work for fragile
> (closed) and internal/private/fileprivate enums?
>
>
> That's a great question.  I think it would have to have that limitation.
> Using Jordan's terminology, by definition a nonexhaustive cannot provide a
> complete list of all values.
>

This one is tougher for me to make a call. I definitely see the point of
view that says that if a nonexhaustive enum doesn't provide a complete
list, then it would make sense to not synthesize it. On the other hand,
some nonexhaustive enums may still benefit from that. For example, I've
been tinkering with wrapping the ICU APIs in Swift, and I have an enum for
Unicode code blocks
.
That would be a good candidate for a nonexhaustive enum because the spec is
always growing, but it would still be very useful to have the compiler
synthesize the collection and count for me (for example, to display in a
table), especially since it is large.



>
>
> TJ
>
>
> On Sep 9, 2017, at 15:23, Matthew Johnson via swift-evolution <
> swift-evolution@swift.org> wrote:
>
>
>
> Sent from my iPad
>
> On Sep 9, 2017, at 7:33 AM, Brent Royal-Gordon 
> wrote:
>
> On Sep 8, 2017, at 5:14 PM, Xiaodi Wu via swift-evolution <
> swift-evolution@swift.org> wrote:
>
> Here, people just want an array of all cases. Give them an array of all
> cases. When it's not possible (i.e., in the case of cases with associated
> values), don't do it.
>
>
> I agree it should be Int-indexed; that seems to be what people want from
> this.
>
> I seem to recall that there is information about the available enum cases
> in the module metadata. If so, and if we're willing to lock that in as part
> of the ABI design, I think we should write—or at least allow for—a custom
> Int-indexed collection, because this may allow us to recurse into
> associated value types. If we aren't going to have suitable metadata,
> though, I agree we should just use an Array. There are pathological cases
> where instantiating a large Array might be burdensome, but sometimes you
> just have to ignore the pathological cases.
>
> (The "infinite recursion" problem with associated values is actually
> relatively easy to solve, by the way: Don't allow, or at least don't
> generate, `ValuesEnumerable` conformance on enums with `indirect` cases.)
>
>
> This is the direction I think makes the most sense in terms of how we
> should approach synthesis.  The open question in my mind is what the exact
> requirement of the protocol should be.  Should it exactly match what we
> synthesize (`[Self]` or an associated `Collection where Iterator.Element ==
> Self, Index == Int`) or whether the protocol should have a more relaxed
> requirement of `Sequence where Iterator.Element == Self` like Tony proposed.
>
>
> --
> Brent Royal-Gordon
> Architechies
>
> ___
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> ___
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-09 Thread Christopher Kornher via swift-evolution

> On Sep 9, 2017, at 12:36 PM, Matthew Johnson via swift-evolution 
>  wrote:
> 
> 
> 
> Sent from my iPad
> 
> On Sep 9, 2017, at 11:42 AM, gs.  > wrote:
> 
>> How does fragility play into this? Does this only work for fragile (closed) 
>> and internal/private/fileprivate enums?
> 
> That's a great question.  I think it would have to have that limitation.  
> Using Jordan's terminology, by definition a nonexhaustive cannot provide a 
> complete list of all values.

The runtime “knows” (or could be made to know) all the cases at any given 
moment in time (ignoring runtime-loaded modules, should they ever be 
supported). This is actually a strong argument for the creation of this 
feature. It would be impossible for such a list to be maintained manually. 
Making the list available somehow at compile time would almost guarantee a 
source-breaking/ABI-breaking change in the future.  This raises a question: 
would models want anything other than the complete list of cases at runtime? 
For example, the module containing the root enum may have a use for the cases 
just defined within that module. I propose that the feature be defined to 
include all cases at runtime and that discussions of partial lists of cases be 
deferred until a use is found for them.

> 
>> 
>> TJ 
>> 
>> On Sep 9, 2017, at 15:23, Matthew Johnson via swift-evolution 
>> > wrote:
>> 
>>> 
>>> 
>>> Sent from my iPad
>>> 
>>> On Sep 9, 2017, at 7:33 AM, Brent Royal-Gordon >> > wrote:
>>> 
> On Sep 8, 2017, at 5:14 PM, Xiaodi Wu via swift-evolution 
> > wrote:
> 
> Here, people just want an array of all cases. Give them an array of all 
> cases. When it's not possible (i.e., in the case of cases with associated 
> values), don't do it.
 
 
 I agree it should be Int-indexed; that seems to be what people want from 
 this.
 
 I seem to recall that there is information about the available enum cases 
 in the module metadata. If so, and if we're willing to lock that in as 
 part of the ABI design, I think we should write—or at least allow for—a 
 custom Int-indexed collection, because this may allow us to recurse into 
 associated value types. If we aren't going to have suitable metadata, 
 though, I agree we should just use an Array. There are pathological cases 
 where instantiating a large Array might be burdensome, but sometimes you 
 just have to ignore the pathological cases.
 
 (The "infinite recursion" problem with associated values is actually 
 relatively easy to solve, by the way: Don't allow, or at least don't 
 generate, `ValuesEnumerable` conformance on enums with `indirect` cases.)
>>> 
>>> This is the direction I think makes the most sense in terms of how we 
>>> should approach synthesis.  The open question in my mind is what the exact 
>>> requirement of the protocol should be.  Should it exactly match what we 
>>> synthesize (`[Self]` or an associated `Collection where Iterator.Element == 
>>> Self, Index == Int`) or whether the protocol should have a more relaxed 
>>> requirement of `Sequence where Iterator.Element == Self` like Tony proposed.
>>> 
 
 -- 
 Brent Royal-Gordon
 Architechies
 
>>> ___
>>> swift-evolution mailing list
>>> swift-evolution@swift.org 
>>> https://lists.swift.org/mailman/listinfo/swift-evolution 
>>> 
> ___
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-09 Thread Matthew Johnson via swift-evolution


Sent from my iPad

> On Sep 9, 2017, at 11:42 AM, gs.  wrote:
> 
> How does fragility play into this? Does this only work for fragile (closed) 
> and internal/private/fileprivate enums?

That's a great question.  I think it would have to have that limitation.  Using 
Jordan's terminology, by definition a nonexhaustive cannot provide a complete 
list of all values.

> 
> TJ 
> 
>> On Sep 9, 2017, at 15:23, Matthew Johnson via swift-evolution 
>>  wrote:
>> 
>> 
>> 
>> Sent from my iPad
>> 
>>> On Sep 9, 2017, at 7:33 AM, Brent Royal-Gordon  
>>> wrote:
>>> 
 On Sep 8, 2017, at 5:14 PM, Xiaodi Wu via swift-evolution 
  wrote:
 
 Here, people just want an array of all cases. Give them an array of all 
 cases. When it's not possible (i.e., in the case of cases with associated 
 values), don't do it.
>>> 
>>> 
>>> I agree it should be Int-indexed; that seems to be what people want from 
>>> this.
>>> 
>>> I seem to recall that there is information about the available enum cases 
>>> in the module metadata. If so, and if we're willing to lock that in as part 
>>> of the ABI design, I think we should write—or at least allow for—a custom 
>>> Int-indexed collection, because this may allow us to recurse into 
>>> associated value types. If we aren't going to have suitable metadata, 
>>> though, I agree we should just use an Array. There are pathological cases 
>>> where instantiating a large Array might be burdensome, but sometimes you 
>>> just have to ignore the pathological cases.
>>> 
>>> (The "infinite recursion" problem with associated values is actually 
>>> relatively easy to solve, by the way: Don't allow, or at least don't 
>>> generate, `ValuesEnumerable` conformance on enums with `indirect` cases.)
>> 
>> This is the direction I think makes the most sense in terms of how we should 
>> approach synthesis.  The open question in my mind is what the exact 
>> requirement of the protocol should be.  Should it exactly match what we 
>> synthesize (`[Self]` or an associated `Collection where Iterator.Element == 
>> Self, Index == Int`) or whether the protocol should have a more relaxed 
>> requirement of `Sequence where Iterator.Element == Self` like Tony proposed.
>> 
>>> 
>>> -- 
>>> Brent Royal-Gordon
>>> Architechies
>>> 
>> ___
>> swift-evolution mailing list
>> swift-evolution@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-09 Thread Matthew Johnson via swift-evolution


Sent from my iPad

> On Sep 9, 2017, at 7:33 AM, Brent Royal-Gordon  wrote:
> 
>> On Sep 8, 2017, at 5:14 PM, Xiaodi Wu via swift-evolution 
>>  wrote:
>> 
>> Here, people just want an array of all cases. Give them an array of all 
>> cases. When it's not possible (i.e., in the case of cases with associated 
>> values), don't do it.
> 
> 
> I agree it should be Int-indexed; that seems to be what people want from this.
> 
> I seem to recall that there is information about the available enum cases in 
> the module metadata. If so, and if we're willing to lock that in as part of 
> the ABI design, I think we should write—or at least allow for—a custom 
> Int-indexed collection, because this may allow us to recurse into associated 
> value types. If we aren't going to have suitable metadata, though, I agree we 
> should just use an Array. There are pathological cases where instantiating a 
> large Array might be burdensome, but sometimes you just have to ignore the 
> pathological cases.
> 
> (The "infinite recursion" problem with associated values is actually 
> relatively easy to solve, by the way: Don't allow, or at least don't 
> generate, `ValuesEnumerable` conformance on enums with `indirect` cases.)

This is the direction I think makes the most sense in terms of how we should 
approach synthesis.  The open question in my mind is what the exact requirement 
of the protocol should be.  Should it exactly match what we synthesize 
(`[Self]` or an associated `Collection where Iterator.Element == Self, Index == 
Int`) or whether the protocol should have a more relaxed requirement of 
`Sequence where Iterator.Element == Self` like Tony proposed.

> 
> -- 
> Brent Royal-Gordon
> Architechies
> 
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-09 Thread Brent Royal-Gordon via swift-evolution
> On Sep 8, 2017, at 5:14 PM, Xiaodi Wu via swift-evolution 
>  wrote:
> 
> Here, people just want an array of all cases. Give them an array of all 
> cases. When it's not possible (i.e., in the case of cases with associated 
> values), don't do it.


I agree it should be Int-indexed; that seems to be what people want from this.

I seem to recall that there is information about the available enum cases in 
the module metadata. If so, and if we're willing to lock that in as part of the 
ABI design, I think we should write—or at least allow for—a custom Int-indexed 
collection, because this may allow us to recurse into associated value types. 
If we aren't going to have suitable metadata, though, I agree we should just 
use an Array. There are pathological cases where instantiating a large Array 
might be burdensome, but sometimes you just have to ignore the pathological 
cases.

(The "infinite recursion" problem with associated values is actually relatively 
easy to solve, by the way: Don't allow, or at least don't generate, 
`ValuesEnumerable` conformance on enums with `indirect` cases.)

-- 
Brent Royal-Gordon
Architechies

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Xiaodi Wu via swift-evolution
On Fri, Sep 8, 2017 at 4:08 PM, Matthew Johnson via swift-evolution <
swift-evolution@swift.org> wrote:

>
> On Sep 8, 2017, at 12:05 PM, Tony Allevato 
> wrote:
>
>
>
> On Fri, Sep 8, 2017 at 9:44 AM Matthew Johnson 
> wrote:
>
>> On Sep 8, 2017, at 11:32 AM, Tony Allevato 
>> wrote:
>>
>>
>>
>> On Fri, Sep 8, 2017 at 8:35 AM Matthew Johnson 
>> wrote:
>>
>>> On Sep 8, 2017, at 9:53 AM, Tony Allevato via swift-evolution <
>>> swift-evolution@swift.org> wrote:
>>>
>>> Thanks for bringing this up, Logan! It's something I've been thinking
>>> about a lot lately after a conversation with some colleagues outside of
>>> this community. Some of my thoughts:
>>>
>>> AFAIK, there are two major use cases here: (1) you need the whole
>>> collection of cases, like in your example, and (2) you just need the number
>>> of cases. The latter seems to occur somewhat commonly when people want to
>>> use an enum to define the sections of, say, a UITableView. They just return
>>> the count from numberOfSections(in:) and then switch over the cases in
>>> their cell-providing methods.
>>>
>>> Because of #2, it would be nice to avoid instantiating the collection
>>> eagerly. (Also because of examples like Jonathan's, where the enum is
>>> large.) If all the user is ever really doing is iterating over them,
>>> there's no need to keep the entire collection in memory. This leads us to
>>> look at Sequence; we could use something like AnySequence to keep the
>>> current case as our state and a transition function to advance to the next
>>> one. If a user needs to instantiate the full array from that sequence they
>>> can do so, but they have to do it explicitly.
>>>
>>> The catch is that Sequence only provides `underestimatedCount`, rather
>>> than `count`. Calling the former would be an awkward API (why is it
>>> underestimated? we know how many cases there are). I suppose we could
>>> create a concrete wrapper for Sequence (PrecountedSequence?) that provides
>>> a `count` property to make that cleaner, and then have
>>> `underestimatedCount` return the same thing if users passed this thing into
>>> a generic operation constrained over Sequence. (The standard library
>>> already has support wrappers like EnumeratedSequence, so maybe this is
>>> appropriate.)
>>>
>>> Another question that would need to be answered is, how should the cases
>>> be ordered? Declaration order seems obvious and straightforward, but if you
>>> have a raw-value enum (say, integers), you could have the declaration order
>>> and the numeric order differ. Maybe that's not a problem. Tying the
>>> iteration order to declaration order also means that the behavior of a
>>> program could change simply by reördering the cases. Maybe that's not a big
>>> problem either, but it's something to call out.
>>>
>>> If I were designing this, I'd start with the following approach. First,
>>> add a new protocol to the standard library:
>>>
>>> ```
>>> public protocol ValueEnumerable {
>>>   associatedtype AllValuesSequence: Sequence where
>>> AllValuesSequence.Iterator.Element == Self
>>>
>>>   static var allValues: AllValuesSequence { get }
>>> }
>>> ```
>>>
>>> Then, for enums that declare conformance to that protocol, synthesize
>>> the body of `allValues` to return an appropriate sequence. If we imagine a
>>> model like AnySequence, then the "state" can be the current case, and the
>>> transition function can be a switch/case that returns it and advances to
>>> the next one (finally returning nil).
>>>
>>> There's an opportunity for optimization that may or may not be worth it:
>>> if the enum is RawRepresentable with RawValue == Int, AND all the raw
>>> values are in a contiguous range, AND declaration order is numerical order
>>> (assuming we kept that constraint), then the synthesized state machine can
>>> just be a simple integer incrementation and call to `init?(rawValue:)`.
>>> When all the cases have been generated, that will return nil on its own.
>>>
>>> So that covers enums without associated values. What about those with
>>> associated values? I would argue that the "number of cases" isn't something
>>> that's very useful here—if we consider that enum cases are really factory
>>> functions for concrete values of the type, then we shouldn't think about
>>> "what are all the cases of this enum" but "what are all the values of this
>>> type". (For enums without associated values, those are synonymous.)
>>>
>>> An enum with associated values can potentially have an infinite number
>>> of values. Here's one:
>>>
>>> ```
>>> enum BinaryTree {
>>>   case subtree(left: BinaryTree, right: BinaryTree)
>>>   case leaf
>>>   case empty
>>> }
>>> ```
>>>
>>> Even without introducing an Element type in the leaf nodes, there are a
>>> countably infinite number of binary trees. So first off, we wouldn't be
>>> able to generate a meaningful `count` property for that. Since 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Matthew Johnson via swift-evolution

> On Sep 8, 2017, at 12:05 PM, Tony Allevato  wrote:
> 
> 
> 
> On Fri, Sep 8, 2017 at 9:44 AM Matthew Johnson  > wrote:
>> On Sep 8, 2017, at 11:32 AM, Tony Allevato > > wrote:
>> 
>> 
>> 
>> On Fri, Sep 8, 2017 at 8:35 AM Matthew Johnson > > wrote:
>>> On Sep 8, 2017, at 9:53 AM, Tony Allevato via swift-evolution 
>>> > wrote:
>>> 
>>> Thanks for bringing this up, Logan! It's something I've been thinking about 
>>> a lot lately after a conversation with some colleagues outside of this 
>>> community. Some of my thoughts:
>>> 
>>> AFAIK, there are two major use cases here: (1) you need the whole 
>>> collection of cases, like in your example, and (2) you just need the number 
>>> of cases. The latter seems to occur somewhat commonly when people want to 
>>> use an enum to define the sections of, say, a UITableView. They just return 
>>> the count from numberOfSections(in:) and then switch over the cases in 
>>> their cell-providing methods.
>>> 
>>> Because of #2, it would be nice to avoid instantiating the collection 
>>> eagerly. (Also because of examples like Jonathan's, where the enum is 
>>> large.) If all the user is ever really doing is iterating over them, 
>>> there's no need to keep the entire collection in memory. This leads us to 
>>> look at Sequence; we could use something like AnySequence to keep the 
>>> current case as our state and a transition function to advance to the next 
>>> one. If a user needs to instantiate the full array from that sequence they 
>>> can do so, but they have to do it explicitly.
>>> 
>>> The catch is that Sequence only provides `underestimatedCount`, rather than 
>>> `count`. Calling the former would be an awkward API (why is it 
>>> underestimated? we know how many cases there are). I suppose we could 
>>> create a concrete wrapper for Sequence (PrecountedSequence?) that provides 
>>> a `count` property to make that cleaner, and then have 
>>> `underestimatedCount` return the same thing if users passed this thing into 
>>> a generic operation constrained over Sequence. (The standard library 
>>> already has support wrappers like EnumeratedSequence, so maybe this is 
>>> appropriate.)
>>> 
>>> Another question that would need to be answered is, how should the cases be 
>>> ordered? Declaration order seems obvious and straightforward, but if you 
>>> have a raw-value enum (say, integers), you could have the declaration order 
>>> and the numeric order differ. Maybe that's not a problem. Tying the 
>>> iteration order to declaration order also means that the behavior of a 
>>> program could change simply by reördering the cases. Maybe that's not a big 
>>> problem either, but it's something to call out.
>>> 
>>> If I were designing this, I'd start with the following approach. First, add 
>>> a new protocol to the standard library:
>>> 
>>> ```
>>> public protocol ValueEnumerable {
>>>   associatedtype AllValuesSequence: Sequence where 
>>> AllValuesSequence.Iterator.Element == Self
>>> 
>>>   static var allValues: AllValuesSequence { get }
>>> }
>>> ```
>>> 
>>> Then, for enums that declare conformance to that protocol, synthesize the 
>>> body of `allValues` to return an appropriate sequence. If we imagine a 
>>> model like AnySequence, then the "state" can be the current case, and the 
>>> transition function can be a switch/case that returns it and advances to 
>>> the next one (finally returning nil).
>>> 
>>> There's an opportunity for optimization that may or may not be worth it: if 
>>> the enum is RawRepresentable with RawValue == Int, AND all the raw values 
>>> are in a contiguous range, AND declaration order is numerical order 
>>> (assuming we kept that constraint), then the synthesized state machine can 
>>> just be a simple integer incrementation and call to `init?(rawValue:)`. 
>>> When all the cases have been generated, that will return nil on its own.
>>> 
>>> So that covers enums without associated values. What about those with 
>>> associated values? I would argue that the "number of cases" isn't something 
>>> that's very useful here—if we consider that enum cases are really factory 
>>> functions for concrete values of the type, then we shouldn't think about 
>>> "what are all the cases of this enum" but "what are all the values of this 
>>> type". (For enums without associated values, those are synonymous.)
>>> 
>>> An enum with associated values can potentially have an infinite number of 
>>> values. Here's one:
>>> 
>>> ```
>>> enum BinaryTree {
>>>   case subtree(left: BinaryTree, right: BinaryTree)
>>>   case leaf
>>>   case empty
>>> }
>>> ```
>>> 
>>> Even without introducing an Element type in the leaf nodes, there are a 
>>> countably infinite number of binary trees. So first 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Logan Shire via swift-evolution
Hey folks!

Thanks for all the great feedback and discussion.
I really like Tony's suggestion of the opt-in ValueEnumerable protocol.
However, I think Kevin is right that it should be a simple array.
If we wanted to get fancy, we could implement a custom integer indexed
collection that indexed into the type's memory to provide a random
access collection of cases without allocating any new memory.
This does seem more complex than just synthesizing the array as
an opt-in, though.

I also agree that it should be in declaration order. If the user wants to
have integer enums in ascending order they can write an extension
that sorts allCases. And I think this should absolutely not work for
associated values. You can implement the protocol if you want,
but you don't get anything synthesized for you.

Best,
Logan

On Fri, Sep 8, 2017 at 11:49 AM Kevin Nattinger via swift-evolution <
swift-evolution@swift.org> wrote:

> I've been waiting for this for years. Literally since Swift was announced.
> IMO it's one of several major gaps in the language.
>
> Some thoughts:
> - It should be a simple array.
> - By far the most simple solution, and the one I (and, I'd guess, others)
> would expect.
> - Every time I've needed the count, I need the actual values either within
> the same method or within the same run loop (e.g. table view).
> - In my experience, direct indexing into the values array is more common
> than iterating over it.
> - Rarely more than a few cases.
> - Even an enum with a lot of cases, the actual number is trivial compared
> to the program's memory usage.
> - No compiler magic beyond spitting out the static array. How do you
> propose to generate on-demand a sequence of values without compiler magic
> and without the values being stored somewhere?
> - It should always be in declaration order. Period.
> - Reordering only in the case of Int raw values (especially only when 0-N
> are all covered) is unintuitive at best, and I expect rarely what the dev
> would want.
> - Should only work on enums without associated values. Enumerating on
> associated values is just begging for trouble.
> - (Infinite) recursion, as you pointed out.
> - It seems more intuitive to me to have enumerated cases grouped, but what
> if there are cases with and without ATs?
>
> [...]
>>
>> Great points! I was only considering the table view/section case where
>> the enum had raw values 0..> possible that someone could just define `enum Section { case header,
>> content, footer }` and then want to turn an IndexPath value into the
>> appropriate Section.
>>
>>
>> On the other hand, though, isn't that what raw value enums are for? If
>> the user needs to do what you're saying—map specific integers to enum
>> values—shouldn't they do so by giving those cases raw values and calling
>> init?(rawValue:), not by indexing into a collection? Especially since they
>> can already do that today, and the only thing they're missing is being able
>> to retrieve the count, which a "PrecountedSequence" mentioned above, or
>> something like it, could also provide.
>>
>> I... guess one could do that? Seems undesirable to me. What if I have
> them explicitly numbered, and delete one? Oops, now the whole thing is off
> and I have to go fix all the numbers. Besides, what if the dev wants to use
> it as a different raw representable? (`enum Title: String { ... }`)?
>
> First, I’m making observations about what people are doing, not what they
>> could do.
>>
>> Second, the raw value may not correspond to 0-based indices.  It might
>> not even be an Int.  There is no reason to couple this common use case of
>> `allValues` to `Int` raw values with 0-based indices.
>>
> +1000. There is absolutely no reason to join these, or special-case 0-N
> int raw representables.
>
> Do we know of any examples where a user is both (1) defining an enum with
> integer raw values that are noncontiguous or non-zero-based and (2) need
> declaration-ordinal-based indexing into those cases for other reasons, like
> a table/collection view? I can't think of why someone would do that, but
> I'm happy to consider something that I'm missing.
>
> Some underlying meaning? E.g. not a table view, but values or identifiers
> for some sort of low-level protocol.
>
> Third, `init(rawValue:)` is a failable initializer and would require a
>> force unwrap.  If the raw values *are* 0-based integers this is similar to
>> the collection bounds check that would be necessary, but it moves it into
>> user code.  People don’t like writing force unwraps.
>>
>
> Yeah, this is a really good point that I wasn't fully considering. If
> other invariants in the application hold—such as table view cell functions
> never receiving a section index outside 0.. forces users to address a situation that will never actually occur unless
> UIKit is fundamentally broken.
>
> Or the user makes a mistake numbering cases, or forgets to update
> 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Kevin Nattinger via swift-evolution
I've been waiting for this for years. Literally since Swift was announced. IMO 
it's one of several major gaps in the language.

Some thoughts:
- It should be a simple array. 
- By far the most simple solution, and the one I (and, I'd guess, 
others) would expect.
- Every time I've needed the count, I need the actual values either 
within the same method or within the same run loop (e.g. table view).
- In my experience, direct indexing into the values array is more 
common than iterating over it.
- Rarely more than a few cases.
- Even an enum with a lot of cases, the actual number is trivial 
compared to the program's memory usage.
- No compiler magic beyond spitting out the static array. How do you 
propose to generate on-demand a sequence of values without compiler magic and 
without the values being stored somewhere?
- It should always be in declaration order. Period. 
- Reordering only in the case of Int raw values (especially only when 
0-N are all covered) is unintuitive at best, and I expect rarely what the dev 
would want.
- Should only work on enums without associated values. Enumerating on 
associated values is just begging for trouble. 
- (Infinite) recursion, as you pointed out.
- It seems more intuitive to me to have enumerated cases grouped, but 
what if there are cases with and without ATs?

>> [...]
>> Great points! I was only considering the table view/section case where the 
>> enum had raw values 0..> could just define `enum Section { case header, content, footer }` and then 
>> want to turn an IndexPath value into the appropriate Section.
>> 
>> On the other hand, though, isn't that what raw value enums are for? If the 
>> user needs to do what you're saying—map specific integers to enum 
>> values—shouldn't they do so by giving those cases raw values and calling 
>> init?(rawValue:), not by indexing into a collection? Especially since they 
>> can already do that today, and the only thing they're missing is being able 
>> to retrieve the count, which a "PrecountedSequence" mentioned above, or 
>> something like it, could also provide.

I... guess one could do that? Seems undesirable to me. What if I have them 
explicitly numbered, and delete one? Oops, now the whole thing is off and I 
have to go fix all the numbers. Besides, what if the dev wants to use it as a 
different raw representable? (`enum Title: String { ... }`)?

> 
> First, I’m making observations about what people are doing, not what they 
> could do.  
> 
> Second, the raw value may not correspond to 0-based indices.  It might not 
> even be an Int.  There is no reason to couple this common use case of 
> `allValues` to `Int` raw values with 0-based indices.

+1000. There is absolutely no reason to join these, or special-case 0-N int raw 
representables. 

> 
> Do we know of any examples where a user is both (1) defining an enum with 
> integer raw values that are noncontiguous or non-zero-based and (2) need 
> declaration-ordinal-based indexing into those cases for other reasons, like a 
> table/collection view? I can't think of why someone would do that, but I'm 
> happy to consider something that I'm missing.

Some underlying meaning? E.g. not a table view, but values or identifiers for 
some sort of low-level protocol.

>  
> 
> Third, `init(rawValue:)` is a failable initializer and would require a force 
> unwrap.  If the raw values *are* 0-based integers this is similar to the 
> collection bounds check that would be necessary, but it moves it into user 
> code.  People don’t like writing force unwraps.
> 
> Yeah, this is a really good point that I wasn't fully considering. If other 
> invariants in the application hold—such as table view cell functions never 
> receiving a section index outside 0.. users to address a situation that will never actually occur unless UIKit is 
> fundamentally broken.

Or the user makes a mistake numbering cases, or forgets to update something, 
... I usually put an assertion failure there, but I hate relying on even system 
libraries in production code. 

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Tony Allevato via swift-evolution
On Fri, Sep 8, 2017 at 9:44 AM Matthew Johnson 
wrote:

> On Sep 8, 2017, at 11:32 AM, Tony Allevato 
> wrote:
>
>
>
> On Fri, Sep 8, 2017 at 8:35 AM Matthew Johnson 
> wrote:
>
>> On Sep 8, 2017, at 9:53 AM, Tony Allevato via swift-evolution <
>> swift-evolution@swift.org> wrote:
>>
>> Thanks for bringing this up, Logan! It's something I've been thinking
>> about a lot lately after a conversation with some colleagues outside of
>> this community. Some of my thoughts:
>>
>> AFAIK, there are two major use cases here: (1) you need the whole
>> collection of cases, like in your example, and (2) you just need the number
>> of cases. The latter seems to occur somewhat commonly when people want to
>> use an enum to define the sections of, say, a UITableView. They just return
>> the count from numberOfSections(in:) and then switch over the cases in
>> their cell-providing methods.
>>
>> Because of #2, it would be nice to avoid instantiating the collection
>> eagerly. (Also because of examples like Jonathan's, where the enum is
>> large.) If all the user is ever really doing is iterating over them,
>> there's no need to keep the entire collection in memory. This leads us to
>> look at Sequence; we could use something like AnySequence to keep the
>> current case as our state and a transition function to advance to the next
>> one. If a user needs to instantiate the full array from that sequence they
>> can do so, but they have to do it explicitly.
>>
>> The catch is that Sequence only provides `underestimatedCount`, rather
>> than `count`. Calling the former would be an awkward API (why is it
>> underestimated? we know how many cases there are). I suppose we could
>> create a concrete wrapper for Sequence (PrecountedSequence?) that provides
>> a `count` property to make that cleaner, and then have
>> `underestimatedCount` return the same thing if users passed this thing into
>> a generic operation constrained over Sequence. (The standard library
>> already has support wrappers like EnumeratedSequence, so maybe this is
>> appropriate.)
>>
>> Another question that would need to be answered is, how should the cases
>> be ordered? Declaration order seems obvious and straightforward, but if you
>> have a raw-value enum (say, integers), you could have the declaration order
>> and the numeric order differ. Maybe that's not a problem. Tying the
>> iteration order to declaration order also means that the behavior of a
>> program could change simply by reördering the cases. Maybe that's not a big
>> problem either, but it's something to call out.
>>
>> If I were designing this, I'd start with the following approach. First,
>> add a new protocol to the standard library:
>>
>> ```
>> public protocol ValueEnumerable {
>>   associatedtype AllValuesSequence: Sequence where
>> AllValuesSequence.Iterator.Element == Self
>>
>>   static var allValues: AllValuesSequence { get }
>> }
>> ```
>>
>> Then, for enums that declare conformance to that protocol, synthesize the
>> body of `allValues` to return an appropriate sequence. If we imagine a
>> model like AnySequence, then the "state" can be the current case, and the
>> transition function can be a switch/case that returns it and advances to
>> the next one (finally returning nil).
>>
>> There's an opportunity for optimization that may or may not be worth it:
>> if the enum is RawRepresentable with RawValue == Int, AND all the raw
>> values are in a contiguous range, AND declaration order is numerical order
>> (assuming we kept that constraint), then the synthesized state machine can
>> just be a simple integer incrementation and call to `init?(rawValue:)`.
>> When all the cases have been generated, that will return nil on its own.
>>
>> So that covers enums without associated values. What about those with
>> associated values? I would argue that the "number of cases" isn't something
>> that's very useful here—if we consider that enum cases are really factory
>> functions for concrete values of the type, then we shouldn't think about
>> "what are all the cases of this enum" but "what are all the values of this
>> type". (For enums without associated values, those are synonymous.)
>>
>> An enum with associated values can potentially have an infinite number of
>> values. Here's one:
>>
>> ```
>> enum BinaryTree {
>>   case subtree(left: BinaryTree, right: BinaryTree)
>>   case leaf
>>   case empty
>> }
>> ```
>>
>> Even without introducing an Element type in the leaf nodes, there are a
>> countably infinite number of binary trees. So first off, we wouldn't be
>> able to generate a meaningful `count` property for that. Since they're
>> countably infinite, we *could* theoretically lazily generate a sequence of
>> them! It would be a true statement to say "an enum with associated values
>> can have all of its values enumerated if all of its associated values are
>> also ValueEnumerable". But I don't think that's 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Matthew Johnson via swift-evolution

> On Sep 8, 2017, at 11:32 AM, Tony Allevato  wrote:
> 
> 
> 
> On Fri, Sep 8, 2017 at 8:35 AM Matthew Johnson  > wrote:
>> On Sep 8, 2017, at 9:53 AM, Tony Allevato via swift-evolution 
>> > wrote:
>> 
>> Thanks for bringing this up, Logan! It's something I've been thinking about 
>> a lot lately after a conversation with some colleagues outside of this 
>> community. Some of my thoughts:
>> 
>> AFAIK, there are two major use cases here: (1) you need the whole collection 
>> of cases, like in your example, and (2) you just need the number of cases. 
>> The latter seems to occur somewhat commonly when people want to use an enum 
>> to define the sections of, say, a UITableView. They just return the count 
>> from numberOfSections(in:) and then switch over the cases in their 
>> cell-providing methods.
>> 
>> Because of #2, it would be nice to avoid instantiating the collection 
>> eagerly. (Also because of examples like Jonathan's, where the enum is 
>> large.) If all the user is ever really doing is iterating over them, there's 
>> no need to keep the entire collection in memory. This leads us to look at 
>> Sequence; we could use something like AnySequence to keep the current case 
>> as our state and a transition function to advance to the next one. If a user 
>> needs to instantiate the full array from that sequence they can do so, but 
>> they have to do it explicitly.
>> 
>> The catch is that Sequence only provides `underestimatedCount`, rather than 
>> `count`. Calling the former would be an awkward API (why is it 
>> underestimated? we know how many cases there are). I suppose we could create 
>> a concrete wrapper for Sequence (PrecountedSequence?) that provides a 
>> `count` property to make that cleaner, and then have `underestimatedCount` 
>> return the same thing if users passed this thing into a generic operation 
>> constrained over Sequence. (The standard library already has support 
>> wrappers like EnumeratedSequence, so maybe this is appropriate.)
>> 
>> Another question that would need to be answered is, how should the cases be 
>> ordered? Declaration order seems obvious and straightforward, but if you 
>> have a raw-value enum (say, integers), you could have the declaration order 
>> and the numeric order differ. Maybe that's not a problem. Tying the 
>> iteration order to declaration order also means that the behavior of a 
>> program could change simply by reördering the cases. Maybe that's not a big 
>> problem either, but it's something to call out.
>> 
>> If I were designing this, I'd start with the following approach. First, add 
>> a new protocol to the standard library:
>> 
>> ```
>> public protocol ValueEnumerable {
>>   associatedtype AllValuesSequence: Sequence where 
>> AllValuesSequence.Iterator.Element == Self
>> 
>>   static var allValues: AllValuesSequence { get }
>> }
>> ```
>> 
>> Then, for enums that declare conformance to that protocol, synthesize the 
>> body of `allValues` to return an appropriate sequence. If we imagine a model 
>> like AnySequence, then the "state" can be the current case, and the 
>> transition function can be a switch/case that returns it and advances to the 
>> next one (finally returning nil).
>> 
>> There's an opportunity for optimization that may or may not be worth it: if 
>> the enum is RawRepresentable with RawValue == Int, AND all the raw values 
>> are in a contiguous range, AND declaration order is numerical order 
>> (assuming we kept that constraint), then the synthesized state machine can 
>> just be a simple integer incrementation and call to `init?(rawValue:)`. When 
>> all the cases have been generated, that will return nil on its own.
>> 
>> So that covers enums without associated values. What about those with 
>> associated values? I would argue that the "number of cases" isn't something 
>> that's very useful here—if we consider that enum cases are really factory 
>> functions for concrete values of the type, then we shouldn't think about 
>> "what are all the cases of this enum" but "what are all the values of this 
>> type". (For enums without associated values, those are synonymous.)
>> 
>> An enum with associated values can potentially have an infinite number of 
>> values. Here's one:
>> 
>> ```
>> enum BinaryTree {
>>   case subtree(left: BinaryTree, right: BinaryTree)
>>   case leaf
>>   case empty
>> }
>> ```
>> 
>> Even without introducing an Element type in the leaf nodes, there are a 
>> countably infinite number of binary trees. So first off, we wouldn't be able 
>> to generate a meaningful `count` property for that. Since they're countably 
>> infinite, we *could* theoretically lazily generate a sequence of them! It 
>> would be a true statement to say "an enum with associated values can have 
>> all of its values enumerated if all of its associated values are also 
>> 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Tony Allevato via swift-evolution
On Fri, Sep 8, 2017 at 8:35 AM Matthew Johnson 
wrote:

> On Sep 8, 2017, at 9:53 AM, Tony Allevato via swift-evolution <
> swift-evolution@swift.org> wrote:
>
> Thanks for bringing this up, Logan! It's something I've been thinking
> about a lot lately after a conversation with some colleagues outside of
> this community. Some of my thoughts:
>
> AFAIK, there are two major use cases here: (1) you need the whole
> collection of cases, like in your example, and (2) you just need the number
> of cases. The latter seems to occur somewhat commonly when people want to
> use an enum to define the sections of, say, a UITableView. They just return
> the count from numberOfSections(in:) and then switch over the cases in
> their cell-providing methods.
>
> Because of #2, it would be nice to avoid instantiating the collection
> eagerly. (Also because of examples like Jonathan's, where the enum is
> large.) If all the user is ever really doing is iterating over them,
> there's no need to keep the entire collection in memory. This leads us to
> look at Sequence; we could use something like AnySequence to keep the
> current case as our state and a transition function to advance to the next
> one. If a user needs to instantiate the full array from that sequence they
> can do so, but they have to do it explicitly.
>
> The catch is that Sequence only provides `underestimatedCount`, rather
> than `count`. Calling the former would be an awkward API (why is it
> underestimated? we know how many cases there are). I suppose we could
> create a concrete wrapper for Sequence (PrecountedSequence?) that provides
> a `count` property to make that cleaner, and then have
> `underestimatedCount` return the same thing if users passed this thing into
> a generic operation constrained over Sequence. (The standard library
> already has support wrappers like EnumeratedSequence, so maybe this is
> appropriate.)
>
> Another question that would need to be answered is, how should the cases
> be ordered? Declaration order seems obvious and straightforward, but if you
> have a raw-value enum (say, integers), you could have the declaration order
> and the numeric order differ. Maybe that's not a problem. Tying the
> iteration order to declaration order also means that the behavior of a
> program could change simply by reördering the cases. Maybe that's not a big
> problem either, but it's something to call out.
>
> If I were designing this, I'd start with the following approach. First,
> add a new protocol to the standard library:
>
> ```
> public protocol ValueEnumerable {
>   associatedtype AllValuesSequence: Sequence where
> AllValuesSequence.Iterator.Element == Self
>
>   static var allValues: AllValuesSequence { get }
> }
> ```
>
> Then, for enums that declare conformance to that protocol, synthesize the
> body of `allValues` to return an appropriate sequence. If we imagine a
> model like AnySequence, then the "state" can be the current case, and the
> transition function can be a switch/case that returns it and advances to
> the next one (finally returning nil).
>
> There's an opportunity for optimization that may or may not be worth it:
> if the enum is RawRepresentable with RawValue == Int, AND all the raw
> values are in a contiguous range, AND declaration order is numerical order
> (assuming we kept that constraint), then the synthesized state machine can
> just be a simple integer incrementation and call to `init?(rawValue:)`.
> When all the cases have been generated, that will return nil on its own.
>
> So that covers enums without associated values. What about those with
> associated values? I would argue that the "number of cases" isn't something
> that's very useful here—if we consider that enum cases are really factory
> functions for concrete values of the type, then we shouldn't think about
> "what are all the cases of this enum" but "what are all the values of this
> type". (For enums without associated values, those are synonymous.)
>
> An enum with associated values can potentially have an infinite number of
> values. Here's one:
>
> ```
> enum BinaryTree {
>   case subtree(left: BinaryTree, right: BinaryTree)
>   case leaf
>   case empty
> }
> ```
>
> Even without introducing an Element type in the leaf nodes, there are a
> countably infinite number of binary trees. So first off, we wouldn't be
> able to generate a meaningful `count` property for that. Since they're
> countably infinite, we *could* theoretically lazily generate a sequence of
> them! It would be a true statement to say "an enum with associated values
> can have all of its values enumerated if all of its associated values are
> also ValueEnumerable". But I don't think that's something we could have the
> compiler synthesize generally: the logic to tie the sequences together
> would be quite complex in the absence of a construct like coroutines/yield,
> and what's worse, the compiler would have to do some deeper analysis to
> avoid 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Matthew Johnson via swift-evolution

> On Sep 8, 2017, at 9:53 AM, Tony Allevato via swift-evolution 
>  wrote:
> 
> Thanks for bringing this up, Logan! It's something I've been thinking about a 
> lot lately after a conversation with some colleagues outside of this 
> community. Some of my thoughts:
> 
> AFAIK, there are two major use cases here: (1) you need the whole collection 
> of cases, like in your example, and (2) you just need the number of cases. 
> The latter seems to occur somewhat commonly when people want to use an enum 
> to define the sections of, say, a UITableView. They just return the count 
> from numberOfSections(in:) and then switch over the cases in their 
> cell-providing methods.
> 
> Because of #2, it would be nice to avoid instantiating the collection 
> eagerly. (Also because of examples like Jonathan's, where the enum is large.) 
> If all the user is ever really doing is iterating over them, there's no need 
> to keep the entire collection in memory. This leads us to look at Sequence; 
> we could use something like AnySequence to keep the current case as our state 
> and a transition function to advance to the next one. If a user needs to 
> instantiate the full array from that sequence they can do so, but they have 
> to do it explicitly.
> 
> The catch is that Sequence only provides `underestimatedCount`, rather than 
> `count`. Calling the former would be an awkward API (why is it 
> underestimated? we know how many cases there are). I suppose we could create 
> a concrete wrapper for Sequence (PrecountedSequence?) that provides a `count` 
> property to make that cleaner, and then have `underestimatedCount` return the 
> same thing if users passed this thing into a generic operation constrained 
> over Sequence. (The standard library already has support wrappers like 
> EnumeratedSequence, so maybe this is appropriate.)
> 
> Another question that would need to be answered is, how should the cases be 
> ordered? Declaration order seems obvious and straightforward, but if you have 
> a raw-value enum (say, integers), you could have the declaration order and 
> the numeric order differ. Maybe that's not a problem. Tying the iteration 
> order to declaration order also means that the behavior of a program could 
> change simply by reördering the cases. Maybe that's not a big problem either, 
> but it's something to call out.
> 
> If I were designing this, I'd start with the following approach. First, add a 
> new protocol to the standard library:
> 
> ```
> public protocol ValueEnumerable {
>   associatedtype AllValuesSequence: Sequence where 
> AllValuesSequence.Iterator.Element == Self
> 
>   static var allValues: AllValuesSequence { get }
> }
> ```
> 
> Then, for enums that declare conformance to that protocol, synthesize the 
> body of `allValues` to return an appropriate sequence. If we imagine a model 
> like AnySequence, then the "state" can be the current case, and the 
> transition function can be a switch/case that returns it and advances to the 
> next one (finally returning nil).
> 
> There's an opportunity for optimization that may or may not be worth it: if 
> the enum is RawRepresentable with RawValue == Int, AND all the raw values are 
> in a contiguous range, AND declaration order is numerical order (assuming we 
> kept that constraint), then the synthesized state machine can just be a 
> simple integer incrementation and call to `init?(rawValue:)`. When all the 
> cases have been generated, that will return nil on its own.
> 
> So that covers enums without associated values. What about those with 
> associated values? I would argue that the "number of cases" isn't something 
> that's very useful here—if we consider that enum cases are really factory 
> functions for concrete values of the type, then we shouldn't think about 
> "what are all the cases of this enum" but "what are all the values of this 
> type". (For enums without associated values, those are synonymous.)
> 
> An enum with associated values can potentially have an infinite number of 
> values. Here's one:
> 
> ```
> enum BinaryTree {
>   case subtree(left: BinaryTree, right: BinaryTree)
>   case leaf
>   case empty
> }
> ```
> 
> Even without introducing an Element type in the leaf nodes, there are a 
> countably infinite number of binary trees. So first off, we wouldn't be able 
> to generate a meaningful `count` property for that. Since they're countably 
> infinite, we *could* theoretically lazily generate a sequence of them! It 
> would be a true statement to say "an enum with associated values can have all 
> of its values enumerated if all of its associated values are also 
> ValueEnumerable". But I don't think that's something we could have the 
> compiler synthesize generally: the logic to tie the sequences together would 
> be quite complex in the absence of a construct like coroutines/yield, and 
> what's worse, the compiler would have to do some deeper analysis to avoid 
> infinite recursion. 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Tony Allevato via swift-evolution
Thanks for bringing this up, Logan! It's something I've been thinking about
a lot lately after a conversation with some colleagues outside of this
community. Some of my thoughts:

AFAIK, there are two major use cases here: (1) you need the whole
collection of cases, like in your example, and (2) you just need the number
of cases. The latter seems to occur somewhat commonly when people want to
use an enum to define the sections of, say, a UITableView. They just return
the count from numberOfSections(in:) and then switch over the cases in
their cell-providing methods.

Because of #2, it would be nice to avoid instantiating the collection
eagerly. (Also because of examples like Jonathan's, where the enum is
large.) If all the user is ever really doing is iterating over them,
there's no need to keep the entire collection in memory. This leads us to
look at Sequence; we could use something like AnySequence to keep the
current case as our state and a transition function to advance to the next
one. If a user needs to instantiate the full array from that sequence they
can do so, but they have to do it explicitly.

The catch is that Sequence only provides `underestimatedCount`, rather than
`count`. Calling the former would be an awkward API (why is it
underestimated? we know how many cases there are). I suppose we could
create a concrete wrapper for Sequence (PrecountedSequence?) that provides
a `count` property to make that cleaner, and then have
`underestimatedCount` return the same thing if users passed this thing into
a generic operation constrained over Sequence. (The standard library
already has support wrappers like EnumeratedSequence, so maybe this is
appropriate.)

Another question that would need to be answered is, how should the cases be
ordered? Declaration order seems obvious and straightforward, but if you
have a raw-value enum (say, integers), you could have the declaration order
and the numeric order differ. Maybe that's not a problem. Tying the
iteration order to declaration order also means that the behavior of a
program could change simply by reördering the cases. Maybe that's not a big
problem either, but it's something to call out.

If I were designing this, I'd start with the following approach. First, add
a new protocol to the standard library:

```
public protocol ValueEnumerable {
  associatedtype AllValuesSequence: Sequence where
AllValuesSequence.Iterator.Element == Self

  static var allValues: AllValuesSequence { get }
}
```

Then, for enums that declare conformance to that protocol, synthesize the
body of `allValues` to return an appropriate sequence. If we imagine a
model like AnySequence, then the "state" can be the current case, and the
transition function can be a switch/case that returns it and advances to
the next one (finally returning nil).

There's an opportunity for optimization that may or may not be worth it: if
the enum is RawRepresentable with RawValue == Int, AND all the raw values
are in a contiguous range, AND declaration order is numerical order
(assuming we kept that constraint), then the synthesized state machine can
just be a simple integer incrementation and call to `init?(rawValue:)`.
When all the cases have been generated, that will return nil on its own.

So that covers enums without associated values. What about those with
associated values? I would argue that the "number of cases" isn't something
that's very useful here—if we consider that enum cases are really factory
functions for concrete values of the type, then we shouldn't think about
"what are all the cases of this enum" but "what are all the values of this
type". (For enums without associated values, those are synonymous.)

An enum with associated values can potentially have an infinite number of
values. Here's one:

```
enum BinaryTree {
  case subtree(left: BinaryTree, right: BinaryTree)
  case leaf
  case empty
}
```

Even without introducing an Element type in the leaf nodes, there are a
countably infinite number of binary trees. So first off, we wouldn't be
able to generate a meaningful `count` property for that. Since they're
countably infinite, we *could* theoretically lazily generate a sequence of
them! It would be a true statement to say "an enum with associated values
can have all of its values enumerated if all of its associated values are
also ValueEnumerable". But I don't think that's something we could have the
compiler synthesize generally: the logic to tie the sequences together
would be quite complex in the absence of a construct like coroutines/yield,
and what's worse, the compiler would have to do some deeper analysis to
avoid infinite recursion. For example, if it used the naïve approach of
generating the elements in declaration order, it would keep drilling down
into the `subtree` case above over and over; it really needs to hit the
base cases first, and requiring the user to order the cases in a certain
way for it to just work at all is a non-starter.

So, enums with associated 

Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Jonathan Hull via swift-evolution
+1000

I once made a country code enum, and creating that array was simple, but took 
forever, and was prone to mistakes.

Thanks,
Jon

> On Sep 8, 2017, at 2:56 AM, Logan Shire via swift-evolution 
>  wrote:
> 
> Googling ‘swift iterate over enum cases’ yields many results of various 
> levels of hackery.
> Obviously it’s trivial to write a computed property that returns an enum’s 
> cases as an
> array, but maintaining that is prone to error. If you add another case, you 
> need to make sure
> you update the array property. For enums without associated types,
> I propose adding a synthesized static var, ‘cases', to the enum’s type. E.g.
> 
> enum Suit: String {
>case spades = "♠"
>case hearts = "♥"
>case diamonds = "♦"
>case clubs = "♣"
> }
> 
> let values = (1…13).map { value in
>switch value {
>case 1: return “A”
>case 11: return “J”
>case 12: return “Q”
>case 13: return “K”
>default: return String(value)
>}
> }
> 
> let cards = values.flatMap { value in Suit.cases.map { “\($0)\(value)"  } }
> 
> Yields [“♠A”, “ ♥ A”, …, “♣K”]
> Thoughts?
> 
> 
> Thanks!
> - Logan Shire
> ___
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] [Pitch] Synthesized static enum property to iterate over cases

2017-09-08 Thread Vladimir.S via swift-evolution

On 08.09.2017 12:56, Logan Shire via swift-evolution wrote:

Googling ‘swift iterate over enum cases’ yields many results of various levels 
of hackery.
Obviously it’s trivial to write a computed property that returns an enum’s 
cases as an
array, but maintaining that is prone to error. If you add another case, you 
need to make sure
you update the array property. For enums without associated types,
I propose adding a synthesized static var, ‘cases', to the enum’s type. E.g.


Yes, this feature was discussed previously, but was not going into formal review for 
different reasons. Personally, I want this feature very much.
Currently, as I understand, if someone will implement this feature in Swift and 
raised a formal review - this addition can be accepted.
So, if someone is ready to implement this in Swift compiler code, we can discuss 
details(again) in this pitch to prepare a formal proposal.


Vladimir.



enum Suit: String {
 case spades = "♠"
 case hearts = "♥"
 case diamonds = "♦"
 case clubs = "♣"
}

let values = (1…13).map { value in
 switch value {
 case 1: return “A”
 case 11: return “J”
 case 12: return “Q”
 case 13: return “K”
 default: return String(value)
 }
}

let cards = values.flatMap { value in Suit.cases.map { “\($0)\(value)"  } }

Yields [“♠A”, “ ♥ A”, …, “♣K”]
Thoughts?


Thanks!
- Logan Shire
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution