On 16 July 2018 at 12:06, Arvids Godjuks <[email protected]> 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]