Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126375 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 9558C1A00BC for ; Tue, 11 Feb 2025 18:12:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739297408; bh=veVUxaBgfonPAwy1UMemsdrFp8qZBrAI5Cu7S1mCVmI=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=aQqRcAPgpCmnX4zPItg9gvhRj3eGztim0Y9v3kCZgqDuGIV4K/MqW36b1zKy2urPI m+Zn1Nv5VBqF77vMrt/b8r1WatuoG2QMM+7HsZY3NwvP6+ZMkKgl94b4K9nBVlBm5G VUDuWYEUqAn5IzkKtvNb8XQmvTVorWEUDn3UvDZ3iJZFlG1sSoNnHNj5ycGK3aIMO4 OBFHHPz7JQtX6rkA3pAZmpjgDbONMdrKbg/DmXPMm/0sTas0IoJZ0U3ReM17T2s2TV k3Ue/6MQ2AuN3rMVzqXi4A5ps6aYohivtJ7CiKTZ6atov/7MX7D3KC5fHjonh0355g DZFBp/WCAyodw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AC76B180051 for ; Tue, 11 Feb 2025 18:10:07 +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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b1-smtp.messagingengine.com (fhigh-b1-smtp.messagingengine.com [202.12.124.152]) (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, 11 Feb 2025 18:10:07 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.stl.internal (Postfix) with ESMTP id 408DF25401A9; Tue, 11 Feb 2025 13:12:50 -0500 (EST) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-01.internal (MEProxy); Tue, 11 Feb 2025 13:12:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc: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=fm1; t=1739297570; x= 1739383970; bh=S8tZ2kESDnB5fk3ewf5GIYKSAgMvDW3nBxOIisrCMRc=; b=O 7LF8+DrFM/s1NnzxWRVMzAZhwg1IvhBDk16EBL52kifz8AwwCxSUmjYTV+ajEubu zD4lNTOOjca93bz/p+LhTkVrIl7i22f4XrJF6kUep6zJfOMGnZJRz3io/V1FunIc zfvfVAQJ0n5V9Xw1z1L7zvTgkV+hzihj03WN+AvOk5cDJYPxiCH4se895hbq/ONB zWKVgoRlwFciIHQs+0Xk07DqGhERSVtzC32C5KfsiuNbNIbzG3aZYngNLPeCdua4 VBpp4xtlmWiXdHMC2B4fX+GNOyRG+6rQix7Zu8Bm+8ZNh8UAnbhzLBBxaJ3mfeD+ 2Ish6/vUjjf5rfRaCTo3g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1739297570; x=1739383970; bh=S8tZ2kESDnB5fk3ewf5GIYKSAgMvDW3nBxO IisrCMRc=; b=GfMd55/CLFZUj3kiZpIKUjLwZplgrOAyoWNSEty1BY1WLribzvp HZBI+8F/g5xHk8TYADXLoO0uUg6lpKCyoFMS45xFOo/uk2I05tTnqdrubuXQ6aBy At+ykICCrYZ5jt3x+5zr3eMfR6y1aXrVclXv28cpMQzSXgZBRwQucRLFMV+rRHGe MwR2ypO5j5826XQY7dcHZZjb3dTLxDuBJ6BjnJliPT1vpw0MfuYHvg4JI/QtAyTH VZCsexi3sDb8/kHtNxkEYOJGauIDGXIoamWQVe12aGYKQi26FQZMrIOSqjWPUB7j Ojpbo3jCkvhs6n4JU78cN04lZSJjcEqNJWw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdegudeikecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughr pefoggffhffvvefkjghfufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgrnh guvghrshdfuceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvghr nhepgfehleejjeevleduteeitdejgfdthfefteegfeegudefhedujeffheegteejkedune cuffhomhgrihhnpeefvheglhdrohhrghdpphhhphhsthgrnhdrohhrghenucevlhhushht vghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgssegsohhtthhlvg gurdgtohguvghspdhnsggprhgtphhtthhopeegpdhmohguvgepshhmthhpohhuthdprhgt phhtthhopehhvghllhhosehfrghiiigrnhgrkhhrrghmrdhmvgdprhgtphhtthhopeelud hlihgrhhhimhesghhmrghilhdrtghomhdprhgtphhtthhopehuuggrlhhtshhovhdrvhgr lhgvnhhtihhnsehgmhgrihhlrdgtohhmpdhrtghpthhtohepihhnthgvrhhnrghlsheslh hishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id A332C780068; Tue, 11 Feb 2025 13:12:49 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Tue, 11 Feb 2025 19:12:28 +0100 To: "Valentin Udaltsov" , "Faizan Akram Dar" Cc: "Mihail Liahimov" <91liahim@gmail.com>, "PHP internals" Message-ID: In-Reply-To: References: Subject: Re: [PHP-DEV] RFC: Not Null Assertion Operator Content-Type: multipart/alternative; boundary=1089eeb2bc5b4af6944273e5585d9511 From: rob@bottled.codes ("Rob Landers") --1089eeb2bc5b4af6944273e5585d9511 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Tue, Feb 11, 2025, at 17:06, Valentin Udaltsov wrote: > On Friday, 7 Feb 2025=E2=80=AFat 10:45, Faizan Akram Dar wrote: >>=20 >>=20 >> On Fri, 7 Feb 2025, 08:30 Mihail Liahimov, <91liahim@gmail.com> wrote: >>> Thank you for your answer. Now I will give examples for better under= standing. >>>=20 >>> Simple examples from Typescript: >>>=20 >>> let foo =3D ... >>> foo!.bar() >>> foo!.someProperty.baz() >>>=20 >>> Examples of potentially using in PHP: >>> Without this operator we writing this code: >>>=20 >>> $foo =3D ... >>>=20 >>> if ($foo =3D=3D=3D null) { >>> throw new FooIsNullException(); >>> } >>>=20 >>> $foo->bar. >>>=20 >>> With this operator: >>>=20 >>> $foo!->bar >>> $foo!->someProperty->method(); >>> $foo!->someProperty->anotherProperty!->method(); >>>=20 >>> I think the postfix operator would be illogical in PHP because my op= erator is similar to the existing nullsafe operator in syntax. And it wo= uld be strange if its syntax were different. >>> Or we can implement both operator syntaxes: prefix for accessing pro= perties, and postfix for use with variables, as in your example. >>>=20 >>> Nullsafe: >>> $foo?->bar; >>> Not null assertion: >>> $foo!->bar; >>>=20 >>> If variable bar would be null - php will throw an exception. But now= i dont know which exception it would be :) >>>=20 >>> This operator will be useful in cases where a null value in a specif= ic place will violate the domain logic. Usually we write either assertio= ns or checks and throw our exceptions for this. But it seems to me that = the not null assertion operator will help avoid writing unnecessary code= . The problem, of course, will be catching errors. It is not clear how t= o catch errors by a specific value. I will think about it. >>=20 >>=20 >> Hi, >>=20 >> I don't see the point of this operator, php doesn't allow any operati= on over null.=20 >>=20 >> See https://3v4l.org/UJ5eg >>=20 >> It's throwing warning (mostly for backwards compatibility), I'd rathe= r have it throw error in next php version.=20 >>=20 >> Static analysis already complain about it, so whats the point of !-> = operator? >> https://phpstan.org/r/c9d6e7b3-ac66-4e91-81e8-af0cafdc976c >>=20 >>=20 >>>=20 >=20 > > Static analysis already complain about it >=20 > That's exactly where `!` is helpful. If at a certain point I am sure t= hat a property must not be null and I want to make that explicit (to bot= h developer and static analyzer), I can use > `assert($obj->prop !=3D=3D null)`. However, that requires an extra lin= e of code. `$obj->prop!->x` would be much prettier. >=20 > -- > Valentin I don't necessarily agree that it is prettier. It also is kinda pointles= s to use in parameter type hints because by default, it cannot be nullab= le unless you explicitly declare it is nullable. Personally, what I would rather see would be making variables non-null b= y default. In other words, no variable can store null unless explicitly = declared as such. Something like this: $canBeNull? =3D null; // ?=3D means the variable on the left can hold nu= ll $cannotBeNull =3D null; // TypeError This gets us closer to typed languages, but still very free-form. You ca= n assign anything you want into either variable, but you can only assign= null to $canBeNull. We already have this, sorta, via parameter types, and it would be nice t= o extend it to the rest of the language as well. =E2=80=94 Rob --1089eeb2bc5b4af6944273e5585d9511 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Tue, Feb 11,= 2025, at 17:06, Valentin Udaltsov wrote:
On Friday, 7= Feb 2025=E2=80=AFat 10:45, Faizan Akram Dar <hello@faizanakram.me> wrote:


On Fri, 7 Feb 2025, 08:30 Mihail Liahimov, <= 91liahim@gmail.c= om> wrote:
Thank you for your answ= er. Now I will give examples for better understanding.
Simple examples from Typescript:

let foo =3D ...
foo!.bar()
foo!.someProper= ty.baz()

Examples of potentially using in P= HP:
Without this operator we writing this code:
<= div>
$foo =3D ...

if ($foo =3D= =3D=3D null) {
    throw new FooIsNullException(= );
}

$foo->bar.
<= div>
With this operator:

$foo= !->bar
$foo!->someProperty->method();
$foo!->someProperty->anotherProperty!->method();

I think the postfix operator would be illogical in PHP= because my operator is similar to the existing nullsafe operator in syn= tax. And it would be strange if its syntax were different.
Or we can implement both operator syntaxes: prefix for accessing proper= ties, and postfix for use with variables, as in your example.
<= div>
Nullsafe:
$foo?->bar;
= Not null assertion:
$foo!->bar;

If variable bar would be null - php will throw an exception. But n= ow i dont know which exception it would be :)

This operator will be useful in cases where a null value in a specifi= c place will violate the domain logic. Usually we write either assertion= s or checks and throw our exceptions for this. But it seems to me that t= he not null assertion operator will help avoid writing unnecessary code.= The problem, of course, will be catching errors. It is not clear how to= catch errors by a specific value. I will think about it.

=
Hi,

I don't see the point of this operator, php doesn't allow= any operation over null. 

See https://3v4l.org/UJ5eg

It's throwing warning (mostly for backwards compatibility= ), I'd rather have it throw error in next php version. 

Static analysis already comp= lain about it, so whats the point of !-> operator?



> Static analysis already complain about it

That's exactly where `!` is helpful. If at a certain p= oint I am sure that a property must not be null and I want to make&= nbsp;that explicit (to both developer and static analyzer), I can use
`assert($obj->prop !=3D=3D null)`. However, that re= quires an extra line of code. `$obj->prop!->x` would be much = prettier.

--
Valentin

<= /div>
I don't necessarily agree that it is prettier. It also is kind= a pointless to use in parameter type hints because by default, it cannot= be nullable unless you explicitly declare it is nullable.

Personally, what I would rather see would be making vari= ables non-null by default. In other words, no variable can store null un= less explicitly declared as such. Something like this:
$canBeNull? =3D null; // ?=3D means the variable on the left= can hold null
$cannotBeNull =3D null; // TypeError
<= div>
This gets us closer to typed languages, but still ver= y free-form. You can assign anything you want into either variable, but = you can only assign null to $canBeNull.

We = already have this, sorta, via parameter types, and it would be nice to e= xtend it to the rest of the language as well.

=E2=80=94 Rob
--1089eeb2bc5b4af6944273e5585d9511--