[swift-evolution] [Proposal] Union Type
Cao Jiannan
frogcjn at 163.com
Fri Jul 1 08:33:19 CDT 2016
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> 写道:
>>
>> 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/4f46b7b3/attachment.html>
More information about the swift-evolution
mailing list