Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117714 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1825 invoked from network); 11 May 2022 12:01:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 May 2022 12:01:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EB13518054C for ; Wed, 11 May 2022 06:40:14 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 11 May 2022 06:40:14 -0700 (PDT) Received: by mail-yb1-f173.google.com with SMTP id m128so4070368ybm.5 for ; Wed, 11 May 2022 06:40:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=i5G0QDjc/sI7ZZoMGD7XPTot7yT9qzhnAduSh+5KkQ0=; b=PucGSIwfe51KfCzQIXImpLGic0TchIx3VHOz5JxHaRtuIVkxRum2z/cyNaRW4yfpdN GcaKgvd28kgqgYaHRO59UHk2d1KOQTcJyoSDzJgxcisrBs5+/CzuXHeEdZTSLGD8JVCH aBLz24XpGO+982C2jq0L0IlCDrCkgZ6uu1Tg3G/967ZrisTXrpAP2RcQ1rhkSzzEVSFC 4iMoManhkDbynw2OJf3ZEkfHpj8z/6AUalkozJNxgiX12FMkA2K58935doXF4MEPF2O2 sRZwXqey8mJz64CiozZ1v/jbCTpujssBHP9TRQPusGAOMVgRjRTkH/frXlGz9A2q/p4t zNSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=i5G0QDjc/sI7ZZoMGD7XPTot7yT9qzhnAduSh+5KkQ0=; b=RT+Iabfvzc4FJxqhcE08eexSzAhDXDuYpE0A/gYbU/5Fnxw3fIM98AHxGfS43AfRU1 SgDbk9Hx9GwK9iZEAMI8j2jQYXwAHZ09EA0P/ROzpw4gDjQ1aH+tQm5VWGGjutwboGW/ Brg7uRcB9B/XUXCpUj1et8oFMSRxD07tGPlJGuy93mmYcZJOX5amcHDFiLm7gn1O1ibm X3cztWLoW1Vmwwteei6eWxS5F2ZFQEQReNM0Oyy/XO7jcqeefEhh3dFokeXsSi1JXg2e YPDTBTzEwQwo7qiN+fSIccrhfQbivsxlHGy1eEnXbDIK7ShhMHxjEz0RZlP7oLBvKBPX zTIw== X-Gm-Message-State: AOAM533OQ9VHcjXEKdV3S7TqvREhACy30LNv2otlPFoE1808CYuo5E7R 25vXhmGbbE4WigLLFnPHSsIRG4S14Dcof+ZAavWXha9O9w== X-Google-Smtp-Source: ABdhPJykjBgWXWZj9XIOrocAcn7ncg23XCift6Fp46Eh/U3emLiAw50MJtIgE+x1rLqC6CzJskoPpm7/llGDkVV4mTA= X-Received: by 2002:a25:2f03:0:b0:648:3d2f:6e66 with SMTP id v3-20020a252f03000000b006483d2f6e66mr22796522ybv.9.1652276414181; Wed, 11 May 2022 06:40:14 -0700 (PDT) MIME-Version: 1.0 References: <5598bad8-4e1a-7638-d3aa-36b02568d1a8@alec.pl> <6277abaf.1c69fb81.64ce9.b86aSMTPIN_ADDED_MISSING@mx.google.com> <95143F62-DDE6-4DCC-82B7-C00C54446DD2@craigfrancis.co.uk> In-Reply-To: <95143F62-DDE6-4DCC-82B7-C00C54446DD2@craigfrancis.co.uk> Date: Wed, 11 May 2022 15:40:02 +0200 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] NULL Coercion Consistency From: guilliam.xavier@gmail.com (Guilliam Xavier) Hi, I can't refrain from replying again... On Sat, May 7, 2022 at 1:30 PM Mel Dafert wrote: > > It is exactly user-defined functions that this RFC introduces breakage for. > The behaviour to throw on null in user-defined functions exists since PHP > 7.0, and is being relied on. Changing these now would introduce behaviour changes > that are harder to find than new type errors. Indeed, that's why in the previous thread (https://externals.io/message/116752#117121) I suggested: """ Or maybe (if going directly from error to implicit coercion is deemed too "risky") the current TypeError in non-strict_types mode (when passing NULL to a user-defined function expecting a scalar) could first be "demoted" to some kind of Notice [who said E_STRICT] in 8.2 (along with reverting the Deprecation added in 8.1 for internal functions) and removed in 9.0? """ On Tue, May 10, 2022 at 1:02 AM Craig Francis wrote: > > On 7 May 2022, at 22:11, Jordan LeDoux wrote: > > > What about `int|float`? Which one would it be coerced to? > > Good question, > > Short answer; today, if you pass the string '4' to a function that uses `int|float`, then it receives `int(4)`: > > [...] > > Longer answer... > > [...] > > That said, and back to your question about `int|float`, because the string '4' is coerced to `int(4)`, I'd prefer NULL to be coerced to `int(0)`. Is there really need for debate here? https://wiki.php.net/rfc/union_types_v2#coercive_typing_mode already says: """ If the exact type of the value is not part of the union, then the target type is chosen in the following order of preference: 1. int 2. float 3. string 4. bool If the type both exists in the union, and the value can be coerced to the type under PHPs existing type checking semantics, then the type is chosen. Otherwise the next type is tried. """ (besides, a numeric string is not the best example, because there's an exception, e.g. the string '4.0' is coerced to `float(4.0)` rather than to `int(4)`). On Tue, May 10, 2022 at 1:13 AM Craig Francis wrote: > > On 8 May 2022, at 12:38, Mark Randall wrote: > > On 08/05/2022 11:48, Jordan LeDoux wrote: > >> This is not the case with null. If you use the unset() function on a > >> variable for example, it will var_dump as null *and* it will pass an > >> is_null() check *and* it will pass a $var === null *and* it will return > >> false for an isset() check. Null loses these qualities if it is cast to > >> *any* scalar. > > > > Fortunately the writing is on the wall for the undefined cases, but that doesn't take away from your argument. > > Yep, I agree, an exception for an undefined variable is probably fine... I'm looking at variables specifically being set to to the NULL value, and how that NULL value is coerced. No need for an exception, accessing an undefined variable already triggers a Warning and is going to throw an Error in PHP 9.0 (https://wiki.php.net/rfc/undefined_variable_error_promotion). PS: about "Java", it has nothing to do; Java type declaration `Foo` (where Foo is a class/interface) is equivalent to PHP `?Foo` (or `Foo|null`) i.e. implicit *nullability* (that you cannot even opt-out; the `@NonNull` annotation only works with SCA/IDE or AOP), the RFC discussed here is about implicit *coercion of null to scalar*. Regards, -- Guilliam Xavier