[swift-evolution] [Proposal] Union Type

Adrian Zubarev adrian.zubarev at devandartist.com
Fri Jul 1 08:36:36 CDT 2016


T_T I’ve never said it will compile (the second time now), I’ve said couple of times its just future bikeshedding with variadic generics from the generics manifesto and variadic enum cases that I come up with.



-- 
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2016 um 15:33:41, Cao Jiannan via swift-evolution (swift-evolution at swift.org) schrieb:

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

Hi,

I'm now officially proposal the union type feature for Swift. Please see:


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"


I would be thankful if someone support this idea and give some advice. Thanks!


--
Jiannan



_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160701/58b21248/attachment.html>


More information about the swift-evolution mailing list