Re: Improving PHP's type system
| From: | Larry Garfield | Date: | Fri, 15 Apr 2016 18:04:21 +0000 |
| Subject: | Re: Improving PHP's type system | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92348@lists.php.net to get a copy of this message | ||
On 4/15/16 12:16 PM, Lin Yo-An wrote:
Andrea Faulds <ajf@ajf.me> 於 2016年4月15日 星期五寫道: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 GarfieldThis 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.