Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117778 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27477 invoked from network); 23 May 2022 20:43:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 May 2022 20:43:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A312F180084 for ; Mon, 23 May 2022 15:25:27 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 23 May 2022 15:25:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1653344725; bh=FzxBZV0XBs78MPudYOYVTxzf5bxNMsEYdfveDiDa+nM=; h=X-UI-Sender-Class:Date:Subject:To:References:From:In-Reply-To; b=jbtzyDYQYQv61zjmFknRUZVnOi1tHbROYagOSe/2vobpxe+BY4+XmvO7YXot+sQ2p MHQArb+DiZjU9/mIYXfryqmYHc2LLLI5A1xpANDiTaLXPS2srnmkNaN3FdJLAbzZiG soS4V1OpKqliNwl1FUEaPjfrYem30O0iz9vfU9RU= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.120] ([24.134.51.41]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N4hzZ-1nkR5G1aI3-011ipZ for ; Tue, 24 May 2022 00:25:25 +0200 Message-ID: <584ac903-4742-0d78-5eed-a795c6b70adb@gmx.net> Date: Tue, 24 May 2022 00:25:25 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Content-Language: en-US To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:X/TjHDY3+XeaCQVcXcLXjygY9TOh4QuYt2TEbJtpi9pgFUSG1YY 7u0PbMN4z6tq676bioaE2GDg2aiWmSe/jlV8xif+FJOpxHq0+JAV+Q0aKXVNNVsaHfO0NCj fnWFN+b/J3cP7VJ0t3ikdapKD5yHZL+J3BCnvg05C8mezJh+ON9K/Tu6UDDIHVE6FpzJTZa sAHCMECaJHw9QYHdL13ug== X-UI-Out-Filterresults: notjunk:1;V03:K0:B3UFb4n1+p0=:+fzX79fVPm5dARrHjPDJ64 4uL2Ktzb1c5dTeqLNTG0VPa7zn/4rJMDs8YbwbszQlq7X7Qhqp15TjvPM1h5/ibKu/9DqIRi5 Rt58/c7dpcTf642+OgM2ie23jWZPX5T4zKHH//AL6pg++QqsC3blgD2WlxknH+eoI0hoo5F2J tRyTThocKZ8R06xVOcOn5MXBezkuMB4djGQjTNjAQORVW/77TU3t4FMJdMGoFKO6E8p2EKMUn 7wPPFa8p4lVuVWWUtuHzBkqCBkMg9MXqb0LfrJkiM7FlyXpnBXSLmLfO5g1SH7p5s6r9zlX1V tZMg9/ZDSHfGPNiEXmgV/umophhZmtQtBGnNv3Trg1YrhRz+aeAKnmX8u9BQvNMxakCKaY9MU oxdyCJ+CxEGRGLTj2VqGebo/QIVMg3XbX/XlMhk/Wv2nooymYfiKJHmzykeS6DPPcw99IH0Zr 0dmGW3Y3g5trerfI72WvdoDegl/4HKRiJNTE1hnDHH3WkRUSSRzS4rNpWW866U0OSNo6S4zyx Sh1pw5ilvQSvz9DFkNFKc7VjzAsFXuyi1ma62iYufLKamZuhvIILdsoR0QPy9GtWx3yWJq63c V6tSOqx4Fe4tF9fQ4ytLHXBoSzcrTDpPqtRHEQY6ygjqZHnyU28jLGH/MFCJQU3AmmcfVADZk Fwk8/gbIK8Xw7+5ss0gQjigs+fjL5Sfwi2WO1a7ZeIRhhttvMc8AP4XTQl00vZcOgZpOLMoc7 2gyuotkqSPwhJNFBXisgyhtgO5tPEdlivWL85czgmiUjJwweZTn49/XEe7K/AXkBqdK5hAVGl INcAKiTSdwmQaRIa0T7VsIuzWqldNH9LjNPEBvHvj9YVFdulo+An321/yalVqYFPiFkUwO9pM QTOWT1TPRWCzca7idx9uhC7Ec0RGDX1uVCZnreU/ByJhYJfVmvISPaQxSYqKr2sRqHRJG3Ehs 7IgL1Ml0khP+ZI34bdqw5wqW77DqdJNZ6//uSmgSD7u5uToYw26i4Df0YU2AsYqj5ULJNgvsS zkUIh6kiEvnwWchWtLseq/Pj20TOBbYSoWKjeeUdgBxFk5m589GXs3Dp1DA/9/12fU2ZcPwcf D1lB4GEJyuNw78CYg1FplMwtejHdzC5gf4pNE1wKbYZqt/uRu+08bjL7A== Subject: Re: [PHP-DEV] [Discussion] Stricter implicit boolean coercions From: a.leathley@gmx.net (Andreas Leathley) On 23.05.22 22:54, G. P. B. wrote: > 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 i= t > 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 t= he > 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 problem is that coercion to a typed boolean is not the same as checking an expression for truthiness. It is somewhat related, but when there is a coercion to a typed boolean only scalar values can be coerced, which makes a big difference. Something like if (['']) is basically the same as if ([''] =3D=3D true) which is why I call it checking for truthiness. Yet $obj->booleanProperty =3D ['']; will lead to a type error. So there is a clear difference there already. I also think the expectation is a different one - something like if ($string) is, as far as I have seen, most often used to check if a string it not empty. Yet if you do $obj->booleanProperty =3D $string; you then seem to specifically want to end up with a boolean type, not do a fuzzy check for truthiness. To me that is a very different situation where I actually find the current behavior surprising, in that is does not matter at all what scalar value I give a typed boolean, it will be happy with anything, and if it was a long string that was converted to boolean, you do possibly lose information. That can easily happen if someone changes a property from a string to boolean, but still assigns it a string somewhere. As soon as you use operators like && or || the intention becomes clearer again: $obj->booleanProperty =3D $string && $string2; There you are not passing the string directly to a boolean property, you are checking an expression, and you can even do $obj->booleanProperty =3D $array && $array2; // cannot pass an array to a boolean, but you can check an array (or two) for truthiness / not being empty > Can this prevent bugs, for sure, but within the function context we alre= ady > have a tool to deal with these sorts of issues with strict_types. But could you not argue the same for integers too? Why when you pass a string to an int parameter does it check if it is numeric, and not auto-coerce "hello" to 0? That is different behavior too, and one can use strict_types or explicit coercions instead. My argument is not that the proposal in this RFC is somewhat elegant or solves deep-rooted issues in the language. Similar to the numeric checks for int parameters I am interested in this for purely practical reasons. If somewhere in your application the integer -376 is passed to a boolean property (and coerced to true), would you not rather know about it? Is it not likely a bug, or at least an oversight? Would it not be easier on developers to know that only 7 scalar values are considered "unambigous" when passing to a typed boolean, and that they would be informed if they pass something else? I already rewrote the passage about raising this to a TypeError, where I state that this should be determined later when there is sufficient experience with these deprecation notices, and for me that is not really urgent - if this just remains an unintrusive deprecation notice for the next 10 years that would be fine with me. But I am convinced this will be a useful notice in many applications, where bugs can be found that otherwise would have stayed hidden for a very long time. I appreciate your feedback, and if I can improve anything about the RFC I am open to ideas. I think your RFCs for the type system have made PHP a much better language, and you seem to think about the big picture of the types in PHP, which is important for the language. This RFC might seem like a niche specific change and it does not affect much of PHP itself, but to me that could be what makes it a useful addition - if it mainly reveals bugs in applications, couldn't that be enough of a reason in favor of it?