Re: [RFC] [VOTE] Union types
| From: | Levi Morrison | Date: | Tue, 14 Jun 2016 19:53:10 +0000 |
| Subject: | Re: [RFC] [VOTE] Union types | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-93974@lists.php.net to get a copy of this message | ||
> I'm personally against Union types because it makes no sense for classes
I've been over this before but I'll repeat it here for completeness:
this is not true. Unions provide a way to discriminate between
potential sets of types. Even among classes there is power in unions.
Consider the idea of encoding a Something or Nothing with methods such
as
map, flatmap, filter, etc.
A Something will return a Something for map, and a Nothing will return
a Nothing. By using inheritance there are two problems:
1. We can create additional subclasses that do not obey the
semantics of the type. Imagine how frustrating a stack that does not
behave like a stack would be; same principle here.
2. We don't have covariant return types so we cannot express that
Nothing::map will return Nothing and Something::map will return
Something.
Using two final classes for Something and Nothing and then doing a
union on them has neither of these downsides. Even if we add return
type covariance we cannot solve problem 1.
Given that there are numerous other uses with built-in types such as
Array | Traversable (iterable), int | string (array key), int | float
(numeric) and so forth it does not make sense to me to special case
these things. Generality is better for language features than special
casing.