Re: [RFC] Optional typehint check for parameters

From: Date: Mon, 16 Jul 2018 11:42:00 +0000
Subject: Re: [RFC] Optional typehint check for parameters
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-102844@lists.php.net to get a copy of this message
On 16 July 2018 at 12:06, Arvids Godjuks <arvids.godjuks@gmail.com> wrote: > Basically, you went wrong when you proposed a switch that controlls > language behavior. To add to that - a switch that probably is not > controllable by code that is running. > While I agree with your general principle here, I would like to raise one point in this proposal's favour, which is that PHP's type hints are effectively assertions - if the value is not of the given type at that point, an error is raised. Assertions are always something you turn off in production builds, with a global switch, to the point of compiling the code as though they don't exist, so it's not unreasonable to have a similar switch to disable run-time type checks. As Nikita points out, this is not true of non-strict scalar hints, so that does raise a complication. I was going to point to Dart as an example where type checks have been implemented this way, but it seems that version 2 has replaced the "checked mode" with stronger static analysis, and run-time checks are automatically inserted only for cases where the safety can't be inferred. This might actually be a more promising optimisation for PHP - perhaps OpCache could mark certain code paths as type-safe (or maybe it already does?) As an exaggerated example, this code only really needs to check the value's type once, not four times: function foo(Bar $bar): Bar { return $bar; } function test(Bar $bar): Bar { return foo($bar); } $something = test($something); Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#102844) next »