[swift-evolution] [Pitch] Enum with generic cases

Jaden Geller jaden.geller at gmail.com
Mon Apr 24 17:24:21 CDT 2017


> On Apr 24, 2017, at 3:21 PM, Kevin Nattinger <swift at nattinger.net> wrote:
> 
> 
>> On Apr 24, 2017, at 3:16 PM, Jaden Geller <jaden.geller at gmail.com <mailto:jaden.geller at gmail.com>> wrote:
>> 
>> 
>>> On Apr 24, 2017, at 2:38 PM, Kevin Nattinger <swift at nattinger.net <mailto:swift at nattinger.net>> wrote:
>>> 
>>>> 
>>>> How can I improve your understanding?
>>>> 
>>> 
>>> 
>>> Given the enum I was using earlier:
>>> 
>>> enum Thing {
>>>     case thingOne<T>(T)
>>>     case thingTwo<T>(T)
>>> }
>>> 
>>> - Write a function that takes a thingOne<String> or thingTwo<Int> but nothing else.
>> 
>> This isn’t possible since generic types introduced on cases are erased in the type of `Thing`.
>> 
>> We can actually already achieve what you want by moving the generics onto the type itself, and this is already possible in Swift! No new features are necessary.
>> 
>> ```
>> enum Thing<T1, T2> {
>>     case thingOne(T1)
>>     case thingTwo(T2)
>> }
>> 
>> func test(_ x: Thing<String, Int>) {
>>     switch x {
>>     case .thingOne(let s):
>>         print("The string has value \(s)!")
>>     case .thingTwo(let i):
>>         print("The int has value \(i)!")
>>     }
>> }
>> ```
> 
> Yes, that was almost exactly my original example. My understanding of the proposal is that it will remove that capability, which I find completely unacceptable.
> 

Hi Kevin,

If that is what is being proposed, I agree that that is entirely unacceptable. I however did not understand the proposal to be removing that capability, but instead I understood that it was simply adding another. That is, `Thing<T1, T2>` would *still* be accepted. This feature is entirely additive.

It would be great if the original proposal author could confirm this.

Thanks,
Jaden Geller

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


More information about the swift-evolution mailing list