On Mon, 16 May 2022 at 16:06, Andreas Leathley <a.leath...@gmx.net> wrote:

> Hello Internals,
>
> After the first discussion about this topic
> (https://externals.io/message/117608) I have created a preliminary
> implementation and an RFC for making implicit boolean type coercions
> more strict:
>
> https://wiki.php.net/rfc/stricter_implicit_boolean_coercions
>
> With this email I'm starting the two week discussion period. I welcome
> any feedback on it and hope to further iron out the implementation if
> needed. I mainly chose the route of introducing a deprecation notice
> because it is in line with other RFCs that have similar goals (like the
> Deprecate implicit non-integer-compatible float to int conversions RFC),
> and it is fairly non-intrusive.
>
> Best regards,
>
> Andreas
>

I'll echo Juliette's comments.
I don't like this RFC as it introduces special coercion semantics for
boolean *only* in a function (+ typed properties (well at least I hope it
impacts typed properties)) context.

The obvious other context is the logical one with conditional statements
and/or boolean operators (&&, ||, etc.) where it is pretty typical to do if
($string) {} and use $string as a boolean value.
Having this be true in some contexts and false in others, depending on the
content of the string is far from ideal.
However, implicit coercion to bool can also arise when doing comparisons
where one of the operands is null or bool.
In this case which semantics should be used?

The proposed behaviour is in stark contrast to the one I brought forward
with the implicit float to int coersions.
As this deprecation occurs in *all* instances (moduli explicit casts).
This is also the reason why we stopped pursuing the deprecating implicit
bool to string RFC as using a boolean value within string
interpolation/echoing is somewhat common and would not reduce the cognitive
burden as one would need to remember yet another case about implicit
coercions.

Even the Coercive types for Function arguments RFC [1] which went further
in differentiating function scalar types declaration and what values would
be accepted did not include such a restriction for bool type declaration.
Where it does propose something somewhat sensible by deprecating float to
bool coercions altogether as comparing equality for floats is far from
ideal due to approximation errors.

Can this prevent bugs, for sure, but within the function context we already
have a tool to deal with these sorts of issues with strict_types.

Best regards,

George P. Banyard

[1] https://wiki.php.net/rfc/coercive_sth

Reply via email to