Den 2022-05-08 kl. 08:52, skrev Aleksander Machniak:
On 07.05.2022 23:11, Jordan LeDoux wrote:
What exactly would be the purpose of `?int` if this RFC was passed? To
pass
the value as null instead of 0? That's it?
Yes. No change here.
What about `int|float`? Which one would it be coerced to?
What happens if you pass FALSE to such an argument? int(0). The same
would happen with NULL.
This pretty radically changes how typing itself
would work in existing user programs. What I'm saying is that for such a
radical BC break you need to provide some compelling justification.
If coercion is possible it should be done as it is done for
bool/int/float/string. Inside the function you can still trust the
variable type is as expected. The only difference is that it will not
throw an error on NULL. Right now you can still use your function
"incorrectly" and not get an error, e.g. if you pass FALSE to it (I
understand NULL might be more common). And, if you use strict_types=1
nothing changes for you.
I don't see this as a radical change. Definitely not more radical than
making the code to throw errors where they were not thrown before (no
matter how long the deprecation period was). So, imo the BC-break
argument is not that strong in this case.
The consistency argument is also not that strong (but here probably more
in favor of the change) because as it was mentioned already current
behavior is consistent with some things and inconsistent with others.
The essential question is whether we want more of "weak-typing coercion"
or not. More would mean that there's no breakage for existing code in
PHP9 (essentially only 8.1 would be "broken" (because of the deprecation
notice) so people could just skip it). No need for ugly code like "$var
?? ''" everywhere is also a win.
It's not only ugly code ;) To make your program/application/library 8.1
compatible using that codepattern requires en effort, but brings close
to zero improvement, except being 8.1 compatible. So the net effect is
negative.
r//Björn L
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php