Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84576 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7061 invoked from network); 11 Mar 2015 21:01:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Mar 2015 21:01:23 -0000 Authentication-Results: pb1.pair.com smtp.mail=theodorejb@outlook.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=theodorejb@outlook.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain outlook.com designates 65.54.190.90 as permitted sender) X-PHP-List-Original-Sender: theodorejb@outlook.com X-Host-Fingerprint: 65.54.190.90 bay004-omc2s15.hotmail.com Received: from [65.54.190.90] ([65.54.190.90:58504] helo=BAY004-OMC2S15.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0C/41-32765-22DA0055 for ; Wed, 11 Mar 2015 16:01:23 -0500 Received: from BAY178-W4 ([65.54.190.124]) by BAY004-OMC2S15.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.22751); Wed, 11 Mar 2015 14:01:20 -0700 X-TMN: [jGhu8rn1+CSHg9alCYQqVSGPZ4QI1TPn] X-Originating-Email: [theodorejb@outlook.com] Message-ID: To: Zeev Suraski CC: "internals@lists.php.net" Date: Wed, 11 Mar 2015 16:01:19 -0500 Importance: Normal In-Reply-To: <76c47c6fbd961958c10bc1a069377a34@mail.gmail.com> References: ,<76c47c6fbd961958c10bc1a069377a34@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 11 Mar 2015 21:01:20.0020 (UTC) FILETIME=[8608DD40:01D05C3E] Subject: RE: [VOTE][RFC] Coercive Scalar Type Hints From: theodorejb@outlook.com (Theodore Brown) On Wednesday=2C March 11 2015 at 1:14 pm Zeev Suraski wrote:=0A= =0A= > I don't believe strict STH would bring any meaningful gains for static=0A= > analysis=2C no. Unlike the JIT/AOT advantages (on the same code) which I= =0A= > believe I've proven cannot be gained=2C I can't prove this one - because = you=0A= > can infer slightly different data from strict type hints. But again=2C in= =0A= > edge cases.=0A= =0A= The benefits of strict typing for static analysis are hardly relegated to= =0A= edge cases. Here's a simple example:=0A= =0A= function getUserPermissions(int $userId): array=0A= function getUsername(int $userId): string=0A= =0A= $userId =3D 10=3B=0A= $user =3D getUsername($userId)=3B=0A= $permissions =3D getUserPermissions($user)=3B=0A= =0A= In strict mode static analysis can tell you that this will fail=2C since a= =0A= string is being passed to a function expecting an int. With coercive STH=2C= =0A= all you know is that it may or may not work.=0A= =0A= >> Strict types can absolutely help - not only for static analysis=2C but a= lso=0A= >> for potential future AOT/JIT development. Why? Because only types need t= o=0A= >> be checked=2C rather than values.=0A= > =0A= > I'm sorry=2C but that is simply wrong. Why? Because types ARE in fact a= =0A= > part of the value in PHP. They're an element of PHP's value structure=0A= > (zval). You do NOT know the value with certainty until runtime=2C because= =0A= > again=2C the type IS in fact a part of the value. The very same type=0A= > inference you can do with strict types you can do with coercive or even= =0A= > with weak types. Again=2C this has been discussed in length in the thread= s=0A= > that took place several weeks ago. I believe even Anthony concluded that= =0A= > for a given piece of code - assuming it's unchanged - there are no AOT/JI= T=0A= > advantages. The theory is that there can be stronger static analysis=2C= =0A= > which will provide more insightful suggestions to the developer to change= =0A= > their code which will in turn result in code that can be JIT'd or AOT'd= =0A= > better. Here too=2C we don't believe that's the case and believe you can= =0A= > provide the same value with coercive type hints.=0A= =0A= Of course you can do the same type inference. The difference is that with= =0A= coercive STH knowing the type isn't enough - you also have to know the=0A= value. A string passed to a function with an int type declaration MAY=0A= succeed or MAY fail. You won't know until the value is checked at runtime.= =0A= However=2C in strict mode a user would be required to pass a value of the= =0A= correct type. There are many cases where PHP can check the types at compile= =0A= time=2C but not the values. Therefore=2C with strict STH it is possible to= =0A= eliminate many runtime value checks and type conversions. For example:=0A= =0A= function getFooByVal(string $val): string=0A= function getBarByFoo(int $foo)=0A= getBarByFoo(getFooByVal("value"))=3B=0A= =0A= With coercive STH a runtime value check is necessary. With strict types=2C = a=0A= failure could be detected at compile time=2C the user would fix their code= =2C=0A= and at runtime no value check is necessary. This is a huge advantage for=0A= AOT/JIT.=0A= =0A= Theodore Brown=0A= =