[swift-evolution] [Proposal] Union Type

Cao Jiannan frogcjn at 163.com
Fri Jul 1 08:35:03 CDT 2016


what if OneOf<A, A>?

duplicate variable compile warning?

> 在 2016年7月1日,20:59,Adrian Z. <notifications at github.com <mailto:notifications at github.com>> 写道:
> 
> Just one simple thing to add:
> 
> If ...T equals 10 then <...T, U> = 10 + 1 = 11 types
> If ...U equals 17 then <T, ...U> = 1 + 17 = 18 types
> If ...T equals 20 and ...Uequals 22 then <...T, ...U> = 20 + 22 = 42 types
>> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub <https://github.com/apple/swift-evolution/pull/404#issuecomment-229940122>, or mute the thread <https://github.com/notifications/unsubscribe/ABsfmhH9yasaWK8vAs17DGXWHV54QuCkks5qRQ9KgaJpZM4JC7-2>.
> 
>> 
> 
>> 在 2016年7月1日,21:33,Cao Jiannan <frogcjn at 163.com> 写道:
>> 
>> I can't compile your code. I'm really not sure your code will pass the complex generic check 🙃
>> 
>> Union version more complex or generic version?
>> I think union version is more normal.
>>> For example:
>>> 
>>> typealias ABC = A | B | C
>>> 
>>> typealias ABCD = ABC | D
>>> 
>>> we just use an existed type ABC to construct ABCD
>>> 
>>> But how about generic wrap?
>>> 
>>> Bikeshedding:
>>> 
>>> // we generate the boundary with `A | B` or directly OneOf<A, B>
>>> enum OneOf<...T> {
>>> 
>>>     ...case $#(T)
>>> 
>>>     // Bikeshedding variadic enum casses:
>>>     // we might need an index to set the value?
>>>     init(index: Int, value: T) {
>>> 
>>>         self = .$index(value)
>>>     }
>>> }
>>> 
>>> /// Usage:
>>> /// A | B | C == OneOf<A, B, C>
>>> func |<T, U>(_: T.Type, _: U.Type) -> OneOf<T, U>.Type {
>>> 
>>>     // I believe the usage of `type` like this was prposed by Joe Groff
>>>     // I also use the proposal to remove `.self` magic here
>>>     return OneOf<T, U>
>>> }
>>> 
>>> // Here is how to merge OneOf into a single dimension
>>> func |<...T, U>(_: OneOf<...T>.Type, _: U.Type) -> OneOf<...T, U>.Type {
>>> 
>>>     // Copy and merge types into the new `OneOf` type
>>>     return OneOf<...T, U>
>>> }
>>> 
>>> func |<T, ...U>(_: T.Type, _: OneOf<...U>.Type) -> OneOf<T, ...U>.Type {
>>> 
>>>     // Copy and merge types into the new `OneOf` type
>>>     return OneOf<T, ...U>
>>> }
>>> 
>>> func |<...T, ...U>(_: OneOf<...T>.Type, _: OneOf<...U>.Type) -> OneOf<...T, ...U>.Type {
>>> 
>>>     // Copy and merge types into the new `OneOf` type
>>>     return OneOf<...T, ...U>
>>> }
>>> Your example will become:
>>> 
>>> typealias ABC = A | B | C // or OneOf<A, B, C>
>>> typealias ABCD = ABC | D // merging lhs OneOf with D to OneOf<A, B, C, D>
>>> Mission accomplished.
>>> 
>>> 
>>>> 在 2016年7月1日,17:06,Cao Jiannan <frogcjn at 163.com <mailto:frogcjn at 163.com>> 写道:
>>>> 
>>>> https://github.com/frogcjn/swift-evolution/blob/master/proposals/xxxx-union-type.md <https://github.com/frogcjn/swift-evolution/blob/master/proposals/xxxx-union-type.md>
>>>> 
>>>> Hi,
>>>> 
>>>> I'm now officially proposal the union type feature for Swift. Please see:
>>>> 
>>>> 
>>>> https://github.com/apple/swift/commit/eb7311de065df7ea332cdde8782cb44f9f4a5121 <https://github.com/apple/swift/commit/eb7311de065df7ea332cdde8782cb44f9f4a5121>
>>>> Introduction
>>>> 
>>>> Add union type grammar, represents the type which is one of other types.
>>>> 
>>>> var stringOrURL: String | URL = "https://www.apple.com <https://www.apple.com/>"
>>>> 
>>>> 
>>>> I would be thankful if someone support this idea and give some advice. Thanks!
>>>> 
>>>> 
>>>> --
>>>> Jiannan
>>>> 
>>>> 
>>> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160701/2cde4a84/attachment.html>


More information about the swift-evolution mailing list