Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92333 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 54037 invoked from network); 15 Apr 2016 15:10:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Apr 2016 15:10:42 -0000 X-Host-Fingerprint: 178.62.40.5 ajf.me Received: from [178.62.40.5] ([178.62.40.5:25698] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 20/53-29891-17401175 for ; Fri, 15 Apr 2016 11:10:42 -0400 Message-ID: <20.53.29891.17401175@pb1.pair.com> To: internals@lists.php.net References: <570E99AC.3090804@fleshgrinder.com> <570EA5EB.8090501@fleshgrinder.com> <570EAB0D.6080706@gmail.com> <570EB67E.8010908@garfieldtech.com> <5B147E88-CC0A-4CBC-A49D-C7FE3BF557C0@zend.com> <6F.C3.12455.94C5F075@pb1.pair.com> <20160414094440.GF19347@phcomp.co.uk> <570FD94F.90703@fleshgrinder.com> <570FE8A9.4020809@gmail.com> Date: Fri, 15 Apr 2016 16:10:38 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0 SeaMonkey/2.40 MIME-Version: 1.0 In-Reply-To: <570FE8A9.4020809@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 178.62.40.5 Subject: Re: [PHP-DEV] Re: Improving PHP's type system From: ajf@ajf.me (Andrea Faulds) Hi Stas, Stanislav Malyshev wrote: >> I don't know what is complicated about "string|Stringable" or "Foo|Bar" >> since it is super self-explanatory. However, I find myself checking the > > It may be self-explanatory for you. It's much less self-explanatory for > somebody just starting to learn. It is also very dangerous - if it's > either Foo or Bar, can you call Foo::stuff on it or not? If it's string > or not string, can you call strlen on it? Etc., etc. It adds a lot of > cognitive load and complicates the whole picture. You may have a > specific use case where it is useful (which we have yet to see btw) but > please remember it's a language with literally millions of use cases and > users. 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. There are some cases where you can't create an interface, but if that's the case, I think it is more worthwhile to look at how we can fix those cases, rather than add what amounts to a hacky workaround. Thanks! -- Andrea Faulds https://ajf.me/