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

Kevin Nattinger swift at nattinger.net
Mon Apr 24 17:21:18 CDT 2017


> On Apr 24, 2017, at 3:16 PM, Jaden Geller <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.

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


More information about the swift-evolution mailing list