On 04/02/2021 17:54, Benjamin Morel wrote:
$age = $_GET['age']; // '25.75'
$x = (int) $foo; // I'd expect a warning, not a silent conversion to 25


If we want to make explicit casts "fussy", that would be a much bigger change. Right now, the following are all valid, with no warnings, even though they are all lossy conversions:

    (int)25.75,
    (int)"25.75",
    (int)"hello",
    (int)"99 red balloons",
    (bool)2,
    (bool)"yes",
    (float)"3.1415 is not pi",
    (string)M_PI, // calling this "lossy" is a bit of a stretch, but it will retain decimal rather than binary precision


There's a case to be made for "strict casts" - I've pointed out before that so-called "strict types" mode encourages users to write code that is actually *less* strict, because explicit casts are very forgiving. However, they should probably be a new syntax, because there will be an ENORMOUS amount of code using deliberately using the existing casts in lossy situations.

Regards,

--
Rowan Tommins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to