[swift-evolution] Union instead of Optional

Leonardo Pessoa me at lmpessoa.com
Mon May 16 10:05:50 CDT 2016


Optional a could be implemented with both enumerated and unions with this
proposal but there should be an Undefined type whose only possible value is
nil. It would make no sense but you could declare a function argument or
return or even a property whose type is Undefined too. And this would lead
to otherwise unnecessary code.

I do agree with the idea of unions but I don't believe optionals would be
better implemented with it,

On Saturday, 14 May 2016, 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-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
>
>
>
>
>
>

-- 
- Leonardo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160516/023848ae/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 屏幕快照 2016-05-15 10.20.36.png
Type: image/png
Size: 110214 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160516/023848ae/attachment.png>


More information about the swift-evolution mailing list