Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92523 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98649 invoked from network); 20 Apr 2016 07:57:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Apr 2016 07:57:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=jesseschalken@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=jesseschalken@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.175 as permitted sender) X-PHP-List-Original-Sender: jesseschalken@gmail.com X-Host-Fingerprint: 209.85.223.175 mail-io0-f175.google.com Received: from [209.85.223.175] ([209.85.223.175:36617] helo=mail-io0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C1/82-14036-66637175 for ; Wed, 20 Apr 2016 03:57:26 -0400 Received: by mail-io0-f175.google.com with SMTP id u185so44377084iod.3 for ; Wed, 20 Apr 2016 00:57:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc; bh=gm1B1u4r/6h/wCReZpekgcTZ9ZzucTxAkj96SU25vHg=; b=eM0CvU84xKnQFvOjUbWESP/KCperPVjZjI5tOwBhDdH3aYpzZVMLkWlOtLvdhFD/8a d8WdmKxAv1W+0LKR05+aTPO9kQBVnS+SG5mjN/JDx0SMzpCQlOenJeiYKM85kMLmrYjj 2KjkULpLg3SVnMbqCbRHQjvBhex+GCkiRPuNaFLO5g/wEPmVZAUnF1VVWUmsuZFH7Qky mZ5kLqAoJ/uGH3XfwJSmIeghD/MScAcxdYwDHhggW6BlkaavAvrhozIub6+goXeBc+Nm uWGEa5ynkRS++BIVaSEG5WnOpokdlNDYDPfHH15KDXxB8/hiHNpee3CR6qPKHk7kkY4P 5pbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:date :message-id:subject:from:to:cc; bh=gm1B1u4r/6h/wCReZpekgcTZ9ZzucTxAkj96SU25vHg=; b=TzVQysjA2MXEW19UVh+SPlBbvbW5HyVMxhg/d4atEZPqZIOV6ihr3Xwz/vB1DanbDN Gdx+45vP+fBqQvxMlR6N5ZzR8R2Of02OA7suvwwIBzgZzIC6k7nUJ9PWq7vp7O1K1VMx cg+7uQtMSl605X52pwrrNLIJVcOAldhWyKTXRdEKA2DxFt7/UlBCSk3hwlw4pVfm48zd /4KHrSLW6AV+sHzfQUWynDyrzF9K+EmCatEb7VbBi8qp8NREYBeqz1h1pCQUrnNPMMVY fFT6ELLZ+vPeS8IOjyiFEIoZsniTMOnaFW47rcuRAM/d6oy9eQLkxUEwY4kpUQ6/yZty LCuw== X-Gm-Message-State: AOPr4FUKNfU0Hrlf6WM6yjN9YPzrRb+oXa7bURaDyRyMR8TuR2A7mXYH9Aryz4otykN4atm4qyv5ZqXLhGW8lg== MIME-Version: 1.0 X-Received: by 10.107.137.101 with SMTP id l98mr8388213iod.31.1461139043252; Wed, 20 Apr 2016 00:57:23 -0700 (PDT) Sender: jesseschalken@gmail.com Received: by 10.79.139.133 with HTTP; Wed, 20 Apr 2016 00:57:22 -0700 (PDT) In-Reply-To: <20.53.29891.17401175@pb1.pair.com> 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> <20.53.29891.17401175@pb1.pair.com> Date: Wed, 20 Apr 2016 17:57:22 +1000 X-Google-Sender-Auth: NcdRfSt50h_4fZbaUR5TWIlL5vk Message-ID: To: Andrea Faulds Cc: PHP internals Content-Type: multipart/alternative; boundary=001a113ed540a425d70530e5f13b Subject: Re: [PHP-DEV] Re: Improving PHP's type system From: me@jesseschalken.com (Jesse Schalken) --001a113ed540a425d70530e5f13b Content-Type: text/plain; charset=UTF-8 Without type annotations: function foo($b) { if (!is_string($b)) { // Ugh, why can't the language enforce this? throw new Exception("needed a string"); } } With type annotations: function foo(string $b) { // I know $b is a string. I don't need to check. :) } Without unions: function foo($b) { if (is_string($b)) { // ... } else if ($b instanceof Bar) { // ... } else { // Ugh, why can't the language enforce this? throw new Exception("needed a string|Bar"); } } With unions: function foo(Bar|string $b) { if (is_string($b)) { // ... } else { // I know $b is a Bar here. I don't need to check. :) } } In both cases, the type annotation has removed the 1 check and the need to throw an exception. It's the exact same benefit. On Sat, Apr 16, 2016 at 1:10 AM, Andrea Faulds wrote: > 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/ > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a113ed540a425d70530e5f13b--