Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122707 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 54E101A009C for ; Thu, 21 Mar 2024 15:26:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711034840; bh=+wSBeHBeAlICO/QDhtZw6XpYG8vkuq5RpCTUihOHnTo=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=GcHvJHy4EeGJCnZG9Xm1qzB/aM9QXOPmIryV6muWnyyXc1xbVqEyLpWA+YfnyFjd9 CAM1fo8bro0qSgLZ/BZ6D/9WVwwpWqYsHm05vKzRA3MEusfXxJ9PNB/DJP5vzq1y6u yLNtnwNQ2bvafduGrtpdKS+exyv8wqGdN0sGObBSLcppTaEzjweDLAsb43XcVmo7xo Ta5oJMQHWfHrM5+9q02WSb+CbDslzH3NnUvpG8rv3NZsAoxcBrj8XPtfjAJu/AEOHN pchh6S1HEHiL05vXMDORRkRjAHzZZacRPAaLjJoFCvQwObNao/opOEoxIcC7lX/1s9 hPrFycsBah5BQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BE96A18003E for ; Thu, 21 Mar 2024 15:27:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,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=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 21 Mar 2024 15:27:19 +0000 (UTC) Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3c38eced701so667473b6e.1 for ; Thu, 21 Mar 2024 08:26:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711034816; x=1711639616; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=FARxBmD2m2M85ni+/ZdfcVU0GHcyXxL9QZf+HyKqME0=; b=HArDeeSV+zh6igO8w+8Xr05P1kDOeo5E7q6/AtjQ+gVjGj7iRKEFn6XyPSide2iGh+ 0B3owgFs6gof9xilqu4zkn3KcWrf2VJNU6QbABv97duiAuy94Rv3jPuKvGunTGWRqJ6D eFaCh3yfEs7IQbULo+b+AhkyUnm7EDZx2XVm5rJG/TOCLJxuhafII9ObCHsgjIn2n4nK x0pbHZBTUrfqEmGy8TOW0NSE0+F/h+YbW04oR1THDc5KqbwNkX7oPxuFlyzW9a3GiCyI XNlYHCRpWPtVCqRSSGSk0lX1NWG2oYRtEY8L+RyXIfVJRdW+CkiPank1IgfWYyV2jJsb BBqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711034816; x=1711639616; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FARxBmD2m2M85ni+/ZdfcVU0GHcyXxL9QZf+HyKqME0=; b=tK0YL1L6I/91fE+b6mz4bY78uFESlG0zs0BCw/n9RBgabKovV4pPHum2R9idkldZQQ s0Jex2IIQYNguBr14wdXLrN3xSi7IW5Aw4n5vgGuYWGaIFPYu67DxOaqtSvtNbtTUgxU MmxuISOsweeL+XptqNDo28XMF+9GUMT6MsjHkZdE0chEJ1AvTmW2vD2z2s9s3jJvRLni C92J1sRProD3g9PK7/q4qBDC4Kxx2hZRGIiP0giugrixE74g/ATAflgNPULcLS6xvzHs CO/yTRh4p2bHUyVPoA4cpJb2kipXHKrK2fAey20+a49Vpk/anGSXUc82mJzkE9VfsZ43 KhuA== X-Gm-Message-State: AOJu0YynBv0UuMhqu+PJo1wR0yF1JFrZfoyB+2VC0YN0Gvoc5UrZVFfZ mfXH9zrulYXUk3eyfEfvfmfDhTm/qiiZwwwhjQzteKk3CmpsH1g38Ftfj9svHZn7Zt8QngF+Eka 4BCSyWnmtjcIQyTyFGwO7IT42n711dUYThAc= X-Google-Smtp-Source: AGHT+IHhrMlpxb1FB+tW9fw0D1g7RPWDoV+QA/eRc58EUnWcO4aEJRg7exl8uok42ajguDcsPQBUnmM603D6E965piE= X-Received: by 2002:a05:6808:1305:b0:3c2:1386:ba0f with SMTP id y5-20020a056808130500b003c21386ba0fmr3105898oiv.44.1711034816143; Thu, 21 Mar 2024 08:26:56 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <3F78A125-1946-42E2-A4F5-A2B282BE2107@rwec.co.uk> <2d7ec203-6e80-445c-94f4-d29ef58743b1@rwec.co.uk> In-Reply-To: <2d7ec203-6e80-445c-94f4-d29ef58743b1@rwec.co.uk> Date: Thu, 21 Mar 2024 16:26:45 +0100 Message-ID: Subject: Re: [PHP-DEV] Proposal: AS assertions To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: landers.robert@gmail.com (Robert Landers) On Thu, Mar 21, 2024 at 12:45=E2=80=AFPM Rowan Tommins [IMSoP] wrote: > > On 20/03/2024 23:05, Robert Landers wrote: > > In other > words, I can't think of a case where you'd actually want a Type|null > and you wouldn't have to check for null anyway. > > > It's not about having to check for null; it's about being able to disting= uish between "a null value, which was one of the expected types" and "a val= ue of an unexpected type". > > That's a distinction which is made everywhere else in the language: param= eter types, return types, property types, will all throw an error if you pa= ss a Foo when a ?Bar was expected, they won't silently coerce it to null. > > > > If you think about it, in this proposal, you could use it in a match: > > // $a is TypeA|TypeB|null > > match (true) { > $a as ?TypeA =3D> 'a', > $a as ?TypeB =3D> 'b', > $a =3D=3D=3D null =3D> 'null', > } > > > That won't work, because match performs a strict comparison, and the as e= xpression won't return a boolean true. You would have to do this: > > match (true) { > (bool)($a as ?TypeA) =3D> 'a', > (bool)($a as ?TypeB) =3D> 'b', > $a =3D=3D=3D null =3D> 'null', > } > > Or this: > > match (true) { > ($a as ?TypeA) !=3D=3D null =3D> 'a', > ($a as ?TypeB) !=3D=3D null =3D> 'b', > $a =3D=3D=3D null =3D> 'null', > } > > > Neither of which is particularly readable. What you're really looking for= in that case is an "is" operator: > > match (true) { > $a is TypeA =3D> 'a', > $a is TypeB =3D> 'b', > $a =3D=3D=3D null =3D> 'null', > } > > Which in the draft pattern matching RFC Ilija linked to can be abbreviate= d to: > > match ($a) is { > TypeA =3D> 'a', > TypeB =3D> 'b', > null =3D> 'null', > } > > > Of course, in simple cases, you can use "instanceof" in place of "is" alr= eady: > > match (true) { > $a instanceof TypeA =3D> 'a', > $a instanceof TypeB =3D> 'b', > $a =3D=3D=3D null =3D> 'null', > } > > > > Including `null` in that type > seems to be that you would get null if no other type matches, since > any variable can be `null`. > > > I can't think of any sense in which "any variable can be null" that is no= t true of any other type you might put in the union. We could interpret Foo= |false as meaning "use false as the fallback"; or Foo|int as "use zero as t= he fallback"; but I don't think that would be sensible. > > In other words, the "or null on failure" part is an option to the "as" ex= pression, it's not part of the type you're checking against. If we only wan= ted to support "null on failure", we could have a different keyword, like "= ?as": > > $bar =3D new Bar; > $bar as ?Foo; // Error > $bar ?as Foo; // null (as fallback) > > $null =3D null; > $null as ?Foo; // null (because it's an accepted value) > $null ?as Foo; // null (as fallback) > > A similar suggestion was made in a previous discussion around nullable ca= sts - to distinguish between (?int)$foo as "cast to nullable int" and (int?= )$foo as "cast to int, with null on error". > > > Note however that combining ?as with ?? is not enough to support "chosen = value on failure": > > $bar =3D new Bar; > $bar ?as ?Foo ?? Foo::createDefault(); // creates default object > > $null =3D null; > $null ?as ?Foo ?? Foo::createDefault(); // also creates default object, e= ven though null is an expected value > > That's why my earlier suggestion was to specify the fallback explicitly: > > $bar =3D new Bar; > $bar as ?Foo else null; // null > $bar as ?Foo else Foo::createDefault(); // default object > > $null =3D null; > $nulll as ?Foo else null; // null > $null as ?Foo else Foo::createDefault(); // also null, because it's an ac= cepted value, so the fallback is not evaluated > > Probably, it should then be an error if the fallback value doesn't meet t= he constraint: > > $bar =3D new Bar; > $bar as Foo else null; // error: fallback value null is not of type Foo > $bar as ?Foo else 42; // error: fallback value 42 is not of type ?Foo > > > > Regards, > -- > Rowan Tommins > [IMSoP] Another way of thinking about is: $x =3D $a as null What do you expect $x to be?