Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-14 Thread Tim Shadel via swift-evolution
With everything quiet on this discussion since my last update removing defaults, I've gone ahead and submitted a pull request (https://github.com/apple/swift-evolution/pull/585 ) with the latest draft. I'm certainly happy to still update it if

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-12 Thread Rien via swift-evolution
+1 Site: http://balancingrock.nl Blog: http://swiftrien.blogspot.com Github: http://github.com/Swiftrien Project: http://swiftfire.nl > On 13 Jan 2017, at 03:01, Tim Shadel via swift-evolution > wrote: > > Fantastic points, all. Let me reply to a few. > > First, the odd situations you ment

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-12 Thread Tim Shadel via swift-evolution
I'm glad you like it! I agree, it does lose the at-a-glace view of all cases. That's probably something you could still get with code-folding in the IDE, but it's still something you give up for this syntax. I would hope the small cost actually helps people stick with the old syntax for small en

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-12 Thread Tim Shadel via swift-evolution
Fantastic points, all. Let me reply to a few. First, the odd situations you mentioned with the defaults were spot on. I had seen them, and thought a bit about it, but having you write it out in code made it obvious that they're pretty harmful. I've removed defaults entirely (see the updated Alt

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-12 Thread Jay Abbott via swift-evolution
Thanks for pointing this out Tim. I had actually read the whole thread but over a few days between other things and must have missed the relevant updates. I think you may be reacting to the very first draft. `extension` isn't used > at all in the newest draft, and no code can exist outside the enu

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-12 Thread Anton Zhilin via swift-evolution
2017-01-12 0:51 GMT+03:00 David Sweeris : > > I don't understand the lines in the struct version where you assign > something to `self`. What is ".invalid", for example? I thought you'd > removed the enum altogether. > I totally overlooked it. That can't be done with protocols, scrap that suggesti

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-12 Thread Tim Shadel via swift-evolution
I think you may be reacting to the very first draft. `extension` isn't used at all in the newest draft, and no code can exist outside the enum. In fact, there's a fair amount of similarity to your idea in the newest proposal. https://gist.github.com/timshadel/5a5a8e085a6fd591483a933e603c2562 >

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-11 Thread Jay Abbott via swift-evolution
I like the idea behind this, but I think the proposed syntax abuses extension, which applies to types not individual cases. Putting the code outside the enum also seems wrong, and it’s difficult to see how the compiler would handle it, in particular ensuring exhaustiveness. So I have come up with a

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-11 Thread David Sweeris via swift-evolution
Sent from my iPhone > On Jan 11, 2017, at 14:57, Anton Zhilin via swift-evolution > wrote: > > Wouldn’t protocols be a better solution in this case? If little to no logic > can be shared between enum cases, why have the enum in the first place? > > Your variant: > > protocol State { > >

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-11 Thread Anton Zhilin via swift-evolution
Wouldn’t protocols be a better solution in this case? If little to no logic can be shared between enum cases, why have the enum in the first place? Your variant: protocol State { mutating func react(to event: Event) } enum AuthenticationState: State, CustomStringConvertible { case inval

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-11 Thread Tony Allevato via swift-evolution
I'll summarize my thoughts for now, after thinking about it for a bit longer: Plusses: + In some cases it's nicer to have the ability to group functionality at each case rather than spread across methods lower in the type definition. I've written enums that were like state machines where being abl

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-11 Thread David Sweeris via swift-evolution
> On Jan 11, 2017, at 08:48, Derrick Ho via swift-evolution > wrote: > > Interesting proposal Tim. So instead of repeating the enum cases multiple > times we repeat the functions multiple times? > > I feel like this merely flipping the complexity but not really getting rid of > it. I think

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-11 Thread Derrick Ho via swift-evolution
Interesting proposal Tim. So instead of repeating the enum cases multiple times we repeat the functions multiple times? I feel like this merely flipping the complexity but not really getting rid of it. On Tue, Jan 10, 2017 at 8:08 PM Tim Shadel via swift-evolution < swift-evolution@swift.org> wrot

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-10 Thread Tim Shadel via swift-evolution
OK. I've taken the most recent changes from this thread and put together a draft for a proposal. https://gist.github.com/timshadel/5a5a8e085a6fd591483a933e603c2562 I'd appreciate your review, especially to ensure I've covered

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread David Waite via swift-evolution
> Enum cases _feel_ like separately defined, but tightly related structs > because each case can have distinct associated values. They have special > privileges that a family of structs doesn't have, like `self = .otherCase`. > Enums are really awesome. I think this is the primary difference

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Tony Allevato via swift-evolution
I like that approach a lot (and it would be nice to use separate labels vs. argument names in the case where they do have labels, too). Enum cases with associated values are really just sugar for static methods on the enum type *anyway* with the added pattern matching abilities, so unifying the sy

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Tim Shadel via swift-evolution
Yeah, that's much nicer than what I just sent! :-D > On Jan 9, 2017, at 2:16 PM, Sean Heber wrote: > > I can’t speak for Tim, but I’d suggest just unifying the case syntax with > functions so they become: > > case foo(_ thing: Int) > > And if you don’t actually need to ever *use* it by name i

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Tim Shadel via swift-evolution
I can see it two different ways: 1) this is optional syntax, and doesn't replace or deprecate any existing syntax, so if you want to use it you have to have a label for the associated value, or 2) you may omit the label if you never reference the associated value within any of the calculated fie

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Sean Heber via swift-evolution
I can’t speak for Tim, but I’d suggest just unifying the case syntax with functions so they become: case foo(_ thing: Int) And if you don’t actually need to ever *use* it by name in your enum properties/functions (if you even have any), then you could leave it out and write it like it is now,

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Tim Shadel via swift-evolution
While my main focus is on the proposal about consolidating cases, I had notes about almost that exact syntax... :-D enum TokenState: State { let id: Int let token: String case loading { let startedAt: Date var description: String { return "Loading to

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Tony Allevato via swift-evolution
Ah, my apologies—the syntax highlighting in the thread was throwing off my e-mail client and I was having trouble reading it. Associated values don't necessarily have to have names: I can write "case .foo(Int)". Since your examples use the associated value label as the name of the value inside the

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Tim Shadel via swift-evolution
There are examples of associated values in the proposed syntax. Which parts should I provide more detail on? > On Jan 9, 2017, at 1:43 PM, Tony Allevato via swift-evolution > wrote: > > While I do like the consolidated syntax more than most of the alternatives > I've seen to address this prob

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Tony Allevato via swift-evolution
While I do like the consolidated syntax more than most of the alternatives I've seen to address this problem, any proposed solution also needs to address how it would work with cases that have associated values. That complicates the syntax somewhat. On Mon, Jan 9, 2017 at 12:37 PM Sean Heber via

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Sean Heber via swift-evolution
> On Jan 9, 2017, at 2:28 PM, Guillaume Lessard via swift-evolution > wrote: > > >> On 9 janv. 2017, at 10:54, Tim Shadel via swift-evolution >> wrote: >> >> Enums get large, and they get complicated because the code for each case >> gets sliced up and scattered across many functions. It b

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Guillaume Lessard via swift-evolution
> On 9 janv. 2017, at 10:54, Tim Shadel via swift-evolution > wrote: > > Enums get large, and they get complicated because the code for each case gets > sliced up and scattered across many functions. It becomes a "one of these > things is not like the other" situation because writing function

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Charles Srstka via swift-evolution
> On Jan 9, 2017, at 11:54 AM, Tim Shadel via swift-evolution > wrote: > > enum OneOnOneField: Int { > > var title: String { return "" } > var placeholder: String { return "" } > var image: UIImage { return UIImage() } > > case agenda { > var title: String { return NSLo

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Sean Heber via swift-evolution
I would like something along these lines. I would suggest going farther and do something like this if possible to avoid repeating the type information all over the place: enum OneOnOneField: Int { let title: String let placeholder: String let image: UIImage case

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Tim Shadel via swift-evolution
It seems like my original (simplified) examples didn't clearly highlight what the problem I'm focused on solving. Enums get large, and they get complicated because the code for each case gets sliced up and scattered across many functions. It becomes a "one of these things is not like the other"

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-09 Thread Karim Nassar via swift-evolution
Although I appreciate the intent, I too find the proposed sugar far more noisy and harder to read than the more typical approach. But: TLDR; Simplifying the accessing of enum associated values would be worthy of some syntactic sugar in my opinion. One area of enums that I’d love to see some s

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-08 Thread Derrick Ho via swift-evolution
Currently we can write a helper method to aid in getting the values inside the enum associated value. Below is a fully working implementation: ``` enum Package { case box(String, Int) case circular(String) var associated: Associated { return Associated(package: self) } struct Associated {

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-08 Thread David Sweeris via swift-evolution
> On Jan 8, 2017, at 06:53, Karim Nassar via swift-evolution > wrote: > > One area of enums that I’d love to see some sugar wrapped around (and perhaps > this has already been discussed previously?) is extracting associated values. > > There are many times where, given an enum like: > > enum

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-08 Thread Karim Nassar via swift-evolution
Although I appreciate the intent, I too find the proposed sugar far more noisy and harder to read than the more typical approach. But: TLDR; Simplifying the accessing of enum associated values would be worthy of some syntactic sugar in my opinion. One area of enums that I’d love to see some s

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-08 Thread Daniel Leping via swift-evolution
+1 as for compleax enums it really does get messy. Leave the current approach in place, though. I'm not sure the syntax is best though. Maybe we could just open a curly brace right after the enum case declaration and put the specific function there. Same way everything out of the braces is conside

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-08 Thread Anton Zhilin via swift-evolution
It would be nice if for each enum case, there was a computed property returning an optional. Then you could do: enum FooBar { case foo(Int) case bar(Int) } let x = FooBar.foo(42) x.foo //=> Optional(42) x.bar //=> nil This feature has been discussed many times, but each time it was cons

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-07 Thread Derrick Ho via swift-evolution
Correct me if I am wrong but currently there are only two ways to extract the value of an associated type. // 1 switch n { case foo(let value): print(value) } // 2 if case foo(let value) = n { print(value) } I think it would be nice to have another way. Maybe a tuple-like pattern. let value = n

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-07 Thread Robert Widmann via swift-evolution
I don't think I've ever wanted to distribute the patterns of a switch statement across multiple files. It seems like you want an enum of enums if the code you're writing needs this kind of chunking. Distributing cases is also far more brittle than the existing local switch; failing to include

Re: [swift-evolution] Consolidate Code for Each Case in Enum

2017-01-06 Thread Rien via swift-evolution
I have mixed feelings about this. I would like to see a solution to the exploding switch statements in an enum, and we have talked about that before on this list. However the solution as suggested seems deficient to me. First, A developer would have to look at an enum “extension” in order to be

[swift-evolution] Consolidate Code for Each Case in Enum

2017-01-06 Thread Tim Shadel via swift-evolution
Idea: Consolidate the Code for Each Case in an Enum ​ # Motivation: ​ Consolidate all code related to a single enum case in one spot. This makes it easier to ensure that all the pieces mesh coherently across that one case. ​ # Background: ​ Enum cases _feel_ like separately defined, but tightly re