Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122723 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 8C4721A009C for ; Fri, 22 Mar 2024 09:46:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711100832; bh=83w1XWgMv1wGFStqS6LBCOhkKZEVaQhY2MuiQa4R2Ws=; h=In-Reply-To:References:Date:From:To:Subject:From; b=UruEs8JKofEuf6xCV2AGijMcmfpiUVIdJa/fTVYpsHVklU+1owLIxGK/8d1uQSQSR BsKe0Vk4ZPEdAdRxvEKTPFM78jeuX6jh4cXPW5nv9N/bhX1GGkKTHamUVBug/LmeVj BEDqYjpN7yg1DQhXb+DZwEt/pYrYgoSitkOAivcjQv8gYHGmDjGOIQDfhOfc8q0kli xZyhU/N1tHINX1G0n4C9U+Yx+Yv89RkpH8U9hdG8VLlNyLuBVy0+fmwRnQWcqxW/Ne dDyd420jpwIkE/scu76DEhxSxhneevAldaybscWaSqrmP1Rd9LGsIsuoVXIadQCl71 GVSbrzE9/OAGg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 14D1218005B for ; Fri, 22 Mar 2024 09:47:11 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,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 fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (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 ; Fri, 22 Mar 2024 09:47:10 +0000 (UTC) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id C76AD138010D for ; Fri, 22 Mar 2024 05:46:47 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute6.internal (MEProxy); Fri, 22 Mar 2024 05:46:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1711100807; x=1711187207; bh=AHvcnwGzEl QSwSgUJg0/eSSVUbQ8JIQeNpI57PnQMyY=; b=Pjn/pwLz5p3nvr1xeQ926QM08s 3pqKMUj+Nu3QRZxgn2zsfMEwSe4FYbb7a3ok5hunITtFRlD3ZoOnNjx0gMAK2Pyr Dk34vCTsVdFEDq7qOUU7UDAkedFjYG9Nh2ShzQjwsIG8LrmCgs3iHEnjgdtkboJg 0qXk7xe3F+2wgbB2AI9oF+S1Pp4HdrqsvfNDQ/ep3fMKQsKWt3ig2cTPvRTDjSGM /QEkolxGqXhUPswFJHCZj8KQiMW/IxX6lxdL4dWvsPs3M2DGFqeMd9F46+SM734M dHrHP4n3wGFrScBgauJT2JQBPtBI9CZJTQ+egrn3K//4uklkdKMTGa65HqqA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1711100807; x=1711187207; bh=AHvcnwGzElQSwSgUJg0/eSSVUbQ8 JIQeNpI57PnQMyY=; b=GX+GlPnfKotAM4417eTSOloaS7alVHulO2S+2S02jNEE B4OB6DhYudOIgTJVGAu6sbCFOo1KF5PtZqbeByxmMTUwWP6NyRYAJqjhpuVmqxd+ CQYGg/BXdwjIMabJ/IXImwLtvcnOpIK0ImFDjtFq1yboo22+0ts73DBSuIS0Ot2K BottIqO7JGQEjKQIhZAgUiIUL7uJdZOzg4eLVwJ9w8LNwaWkQsgtoT5lcB6IeR/U 95/fz4/ci4VJNy0dNGMbJMkcPCp6T5NJ2BHXMxib97li8m9N0R1/Ir8voaWGaOFK D3OKMJ6N9GDipscZ2WuKzjpo660Fa8d2A5QpRAwbSQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddttddgtdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsegrtd erreerredtnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdf uceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpe ejgfffleffudehvdfgfefhkeffgedtgeeltdekleevheevgfeljeegtdegieeiudenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprd hphhhpsehrfigvtgdrtghordhukh X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 663F51700093; Fri, 22 Mar 2024 05:46:47 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-332-gdeb4194079-fm-20240319.002-gdeb41940 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <7d53f2e4-46f3-46f1-89b0-6e1d3b0b2e32@app.fastmail.com> In-Reply-To: References: <3F78A125-1946-42E2-A4F5-A2B282BE2107@rwec.co.uk> <2d7ec203-6e80-445c-94f4-d29ef58743b1@rwec.co.uk> Date: Fri, 22 Mar 2024 09:46:22 +0000 To: internals@lists.php.net Subject: Re: [PHP-DEV] Proposal: AS assertions Content-Type: multipart/alternative; boundary=ca6079d344714da6a72e60237b103a0f From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") --ca6079d344714da6a72e60237b103a0f Content-Type: text/plain On Fri, 22 Mar 2024, at 08:17, Jordi Boggiano wrote: > We perhaps could make sure that as does not throw if used with `??`, or that `??` catches the type error and returns the right-hand expression instead: > So to do a nullable typecast you would do: > > $a as int|float ?? null > While this limits the impact to only expressions combining as with ?? it still has the same fundamental problem: you can't meaningfully use it with a nullable type. As a concrete example, imagine you have an optional $description parameter, and want to ensure any non-null values are converted to string, but keep null unchanged. At first sight, it looks like you could write this: $descString = $description as string|null ?? (string)$description; But this won't work - the ?? swallows the null and turns it into an empty string, which isn't what you wanted. You need some syntax that catches the TypeError, but preserves the null: $descString = $description as string|null else (string)$description; // or $descString = $description as string|null catch (string)$description; // or $descString = $description as string|null default (string)$description; I actually think there are quite a lot of scenarios where that idiom would be useful: $optionalExpiryDateTime = $expiry as ?DateTimeInterface else new DateTimeImmutable($expiry); $optionalUnixTimestamp = $time as ?int else strotime((string)$time); $optionalUnicodeName = $name as ?UnicodeString else new UnicodeString( $name ); etc And once you have that, you don't need anything special for the null case, it's just: $nameString = $name as ?string else null; Regards, -- Rowan Tommins [IMSoP] --ca6079d344714da6a72e60237b103a0f Content-Type: text/html Content-Transfer-Encoding: quoted-printable
On Fri, 22 Mar = 2024, at 08:17, Jordi Boggiano wrote:
We perhaps could= make sure that as does not throw if used with `??`, or that `??` catches the type error and returns the right-hand expression instead:

So to do a nullable typ= ecast you would do:

    $a as int|float ?? null=


While this limits the impact to= only expressions combining as with ?? it still has the same fundamental= problem: you can't meaningfully use it with a nullable type.
<= div>

As a concrete example, imagine you hav= e an optional $description parameter, and want to ensure any non-null va= lues are converted to string, but keep null unchanged.
At first sight, it looks like you could write this:

$descString =3D $description as string|null ?? (str= ing)$description;

But this won't work - the= ?? swallows the null and turns it into an empty string, which isn't wha= t you wanted. You need some syntax that catches the TypeError, but prese= rves the null:

$descString =3D $description= as string|null else (string)$description;
// or
=
$descString =3D $description as string|null catch (string)$descript= ion;
// or
$descString =3D $description as s= tring|null default (string)$description;

I actually think there are quite a lot of scenarios where t= hat idiom would be useful:

$optionalExpiryD= ateTime =3D $expiry as ?DateTimeInterface else new DateTimeImmutable($ex= piry);
$optionalUnixTimestamp =3D $time as ?int else strot= ime((string)$time);
$optionalUnicodeName =3D $name as ?Uni= codeString else new UnicodeString( $name );
etc
<= div>
And once you have that, you don't need anything speci= al for the null case, it's just:

$nameStrin= g =3D $name as ?string else null;

Regards,<= br>
--
Rowan Tommins
[IMSoP]
<= /div> --ca6079d344714da6a72e60237b103a0f--