[swift-evolution] [Pre-Proposal-Discussion] Union Type - Swift 4
Haravikk
swift-evolution at haravikk.me
Fri Aug 19 02:24:05 CDT 2016
I'm a +1 for union types.
My main reason for wanting it is to eliminate (some) function overloads; behind the scenes the compiler may still produce one compiled function per union type (for performance), but at a high level we only need to worry about one implementation, and one call signature, which I think is a good thing.
> On 11 Aug 2016, at 02:28, Cao Jiannan via swift-evolution <swift-evolution at swift.org> wrote:
>
> Hi all,
>
> I want to make a discussion about union type for swift 4.
> See 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>
>
> 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/>"
> Now, if we using the new union type feature, we can declare type conveniently, No other type declaration, and compiler will automatically calculate the common interface.
>
> func input(value: A | B | C) {
> print(value.commonProperty) // type checker will calculate the common interface, developer just use it out of box
> switch value {
> case let value as A:
> // value is type A
> print(value.propertyInA)
> case let value as B:
> // value is type B
> print(value.propertyInB)
> case let value as C:
> // value is type C
> print(value.propertyInC)
> }
> // there is no default case other than A, B or C. we already declared that.
> }
> Note: A, B, C can be either class or protocol, or any other types. This leaves developer more freedom.
>
>
> Impact on existing code
>
> This is a new feature, developer who need declare common type will alter to this new grammar.
> Enum based version optional or IUO will be replaced by Union-based ones. Any optional type will automatically replaced by union type
>
> <https://github.com/frogcjn/swift-evolution/blob/master/proposals/xxxx-union-type.md#detailed-design>_______________________________________________
> 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/20160819/37090af7/attachment.html>
More information about the swift-evolution
mailing list