[swift-evolution] Union instead of Optional
Joe Groff
jgroff at apple.com
Mon May 16 11:10:40 CDT 2016
Unlike sum types, such as Swift enums, unions don't have parametricity, because (T \union Nil) \union Nil == T \union Nil. This means that things like collections containing Optionals don't just work, since it becomes impossible for operations like 'find' to distinguish 'nil' as a value in the container from 'nil' as a "not found" result. Languages without sum types tend to compensate by growing multiple "nil"-like values—ObjC has NSNull, Javascript has undefined and null, VB has Null, Nothing, *and* None, and so on.
-Joe
> On May 14, 2016, at 7:33 PM, Cao Jiannan via swift-evolution <swift-evolution at swift.org> wrote:
>
>
> It’s kind of same idea of TypeScipt 2, at 46:21 in this video.
>
> https://channel9.msdn.com/Events/Build/2016/B881
>
> <屏幕快照 2016-05-15 10.20.36.png>
>
>
>>
>> 2016-2-16 GMT+8 14:36:28
>> Hi all,
>>
>> I think the best way to solve the either problem is to separate it from generic.
>> Optional and Either shouldn’t work the same way of generic type.
>> It’s just a represent of multiple type in one location.
>>
>> Using an old friend, Union in C.
>> union {
>> case firstType
>> case secondType
>> }
>>
>> This is the final solution for the sub typing problem of optional.
>>
>> A == union(A,A)
>> union(A,B) == union(B,A)
>> B == union(B,B)
>>
>> B is subtype of union(A,B)
>> A is subtype of union(A,B)
>> union(A,B,C) is subtype of union(A,B,C,D,…)
>>
>> suppose
>> a is subclass of A
>> b is subclass of B, then
>> union(a,B) is subtype of union(A,B)
>> union(A,b) is subtype of union(A,B)
>> union(a,b) is subtype of union(a,B)
>> union(a,b) is subtype of union(A,b)
>>
>> union can have as many case as possible. e.g., union(A,B,C,D,…)
>>
>> So the Optional<UITableView> should be union(UITableView, None)
>> and Optional<MyTableVIew> should be union(MyTableView, None), which is subclass of union(UITableView, None)
>>
>> This is a final rational solution. I think.
>>
>> -Jiannan
>>
>
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list