On 4/15/16 12:16 PM, Lin Yo-An wrote:
Andrea Faulds <a...@ajf.me> 於 2016年4月15日 星期五寫道:
This is something that particularly concerns me about union types, in that
they reduce type safety. If you have a union type of Foo|Bar for some
variable, then the set of methods you can call on that variable is actually
the intersection, not the union, of the set of methods you can call on Foo
and Bar. Which, unless those two classes share some interface, is probably
an empty set. So there's nothing you can actually do safely with it without
doing checks within the body of the function, and if you're doing that,
then why do we have a type declaration? It's only barely more useful than
omitting a type declaration at all; type declarations are supposed to
prevent you needing to check. On the other hand, if the two classes share
some methods, then either there's an interface you can already use here, or
you can create one. Either way, you don't need a union type.
+1 I agree. Language like Golang use interface to intersect the method
calls, I think that's better.
Go's interfaces are implicit rather than explicit. That's what makes
micro-interfaces like that (which are often single-method) feasible.
Anthony Ferrara proposed "weak interfaces" a few years ago but it was
rejected (or didn't make it to a vote; I don't recall which).
Either way, Foo|Bar is not the main use case for union types. See my
earlier email.
--
--Larry Garfield
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php