Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122695 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 0519A1AD8F6 for ; Tue, 19 Mar 2024 19:38:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1710877148; bh=LU3NyggreschmB69O72jsCt76LBLvUOvwQ4z/txoX20=; h=Date:Subject:To:References:From:In-Reply-To:From; b=OiS0dP6QHZ708ryKBX8sVtij9jXSrRq1KAg2Ng8WQfqAN568k6YxF6Q+WMb5TUPaR GKKUj3T4cIuBIojBukzeceNfba6pkeVIR2kEwrUkQwKRuqEIz6ux+o8+hNQHlwcgF2 nsn1ZV87NvOtUnJXT34xZwJaAhfdUnLQLrZkPYYTk4FTP9HdU+aggZPi208eme9B0g DKSoQaXgSxpyp2EJI8IiolZYBfr2AONtZ+D+7x2fCmBkZhafQF7kURG9SrKc06417/ drQkXrUf+eb4bzv/cNpJ02MfVHbQAyWEr9ggOkMY6hCCdJuIh/H11KD2e2viQwbn8h ibeMK+GYYm85g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EC07D18003F for ; Tue, 19 Mar 2024 19:39:06 +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.9 required=5.0 tests=BAYES_40,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 fhigh2-smtp.messagingengine.com (fhigh2-smtp.messagingengine.com [103.168.172.153]) (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 ; Tue, 19 Mar 2024 19:39:06 +0000 (UTC) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 36CA111400DD for ; Tue, 19 Mar 2024 15:38:45 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 19 Mar 2024 15:38:45 -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=1710877125; x=1710963525; bh=t8XjXl9k2N D0pRgLn0jLOv5OVssGVMLiOxJviiJcThM=; b=jQUo5oB3u7f3y/vL2weVoxeNjo u2GX+3G/j8LWFyb1mZ+ftlo8yUrWWRyQn+dvllAll+O+bicDDXjNVA643qkPHXVL qGWtA6CE9D3BSBYFsV7VQO/wLNdhQBQwosLxNuzYhCijnEI/PLspqHdkXmteccv7 antVXiQisDgYoZmKi61iW1JYX3Df19gEs1opUlTQWPv6W+mh/qoNupcwdbCH2NiM PhopsItXXldDidEfv/6l1z98NIHfkbhvbIVkF30HYSvDuf7eelSUtWjgSiMshjx+ fjUkHpuxPFrcmQFY2rHPVMASLgzJaKiA+U4NX37WEs1jiueY6Sam/kwnCIew== 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=1710877125; x=1710963525; bh=t8XjXl9k2ND0pRgLn0jLOv5OVssG VMLiOxJviiJcThM=; b=DcslMpXHA8kcXQfxdxtl65rtWWiQf2Wp5esf0OPIDENY rjm47cMb6iqFxNfQIMR8EUfcwQdx7TSWWMVzcdg3yvaZbt/BGByQTdD7UyUedkfW ef0qgGoIRw0ZwV0ODSI/xdCrKkzVvlbf1TqMPijjCfIEXGTG3jq1c2aZX6FeAyyZ kCpiRjCZ5TAKbxAeV97n4k+zu32viQdL4WrILGr6zGfSuMzXPbHXk6ONZSwGh/d8 tJ6rn+gs/0OX967uQOnpHi4VDuVUV6WsxVmT2DFDix1yEN3oMfPGaW5ra8Zp9XOO 8YItNRGPAyRu/b0dHmVhDPP+mqi/moG9luI+PI71aw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrledtgddutddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpegtkfffgggfuffvfhfhjgesrgdtre ertddvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhsucglkffoufhorfgnfdcu oehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecuggftrfgrthhtvghrnhephe etleeiiefgueduieeuieffvdevheduueefkeejuefgffeftdeitdegtedtleetnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepihhmshhophdrph hhphesrhifvggtrdgtohdruhhk X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 19 Mar 2024 15:38:44 -0400 (EDT) Content-Type: multipart/alternative; boundary="------------DdFl0b9fxOl1pusPXsukd7Xz" Message-ID: Date: Tue, 19 Mar 2024 19:38:42 +0000 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] Proposal: AS assertions To: internals@lists.php.net References: Content-Language: en-GB In-Reply-To: From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") This is a multi-part message in MIME format. --------------DdFl0b9fxOl1pusPXsukd7Xz Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 19/03/2024 16:24, Robert Landers wrote: > $x = $attributeReflection->newInstance() as ?MyAttribute; > if ($x === null) // do something since the attribute isn't MyAttribute I think reusing nullability for this would be a mistake - ideally, the right-hand side should allow any type, so "$foo as ?Foo" should mean the same as "$foo as Foo|null". A better alternative might be to specify a default when the type didn't match: $x = $attributeReflection->newInstance() as ?MyAttribute else null; if ($x === null) // do something since the attribute isn't MyAttribute Which then also allows you to skip the if statement completely: $x = $attributeReflection->newInstance() as MyAttribute else MyAttribute::createDefault(); That then looks a lot like a limited-use version of syntax for catching an exception inline, which would be nice as a general feature (but I think maybe hard to implement?) $x = somethingThatThrows() catch $someDefaultValue; As well pattern matching, which Ilija mentioned, another adjacent feature is a richer set of casting operators. Currently, we can assert that something is an int; or we can force it to be an int; but we can't easily say "make this an int if safe, but throw otherwise" or "make this an int if safe, but substitute null/$someValue otherwise". I've been considering how we can improve that for a while, but not settled on a firm proposal - there's a lot of different versions we *could* support, so choosing a minimal set is hard. Regards, -- Rowan Tommins [IMSoP] --------------DdFl0b9fxOl1pusPXsukd7Xz Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
On 19/03/2024 16:24, Robert Landers wrote:
$x = $attributeReflection->newInstance() as ?MyAttribute;
if ($x === null) // do something since the attribute isn't MyAttribute


I think reusing nullability for this would be a mistake - ideally, the right-hand side should allow any type, so "$foo as ?Foo" should mean the same as "$foo as Foo|null".


A better alternative might be to specify a default when the type didn't match:

$x = $attributeReflection->newInstance() as ?MyAttribute else null;
if ($x === null) // do something since the attribute isn't MyAttribute

Which then also allows you to skip the if statement completely:

$x = $attributeReflection->newInstance() as MyAttribute else MyAttribute::createDefault();


That then looks a lot like a limited-use version of syntax for catching an exception inline, which would be nice as a general feature (but I think maybe hard to implement?)

$x = somethingThatThrows() catch $someDefaultValue;


As well pattern matching, which Ilija mentioned, another adjacent feature is a richer set of casting operators. Currently, we can assert that something is an int; or we can force it to be an int; but we can't easily say "make this an int if safe, but throw otherwise" or "make this an int if safe, but substitute null/$someValue otherwise".

I've been considering how we can improve that for a while, but not settled on a firm proposal - there's a lot of different versions we *could* support, so choosing a minimal set is hard.


Regards,

-- 
Rowan Tommins
[IMSoP]
--------------DdFl0b9fxOl1pusPXsukd7Xz--