Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126373 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 9C9EE1A00BC for ; Tue, 11 Feb 2025 16:07:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739289858; bh=ebbVx5o/UruYLlwP+JQUKyc0ak9FKa4vP84dsoWU7/c=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Tyy98bNxImLcE9jSh0W0FMb2aURhOmdV2M04qw0xtNjubPfruU+Cf1LEEbsW7QoGG msaW98F5WrUehVSFhoGshkMjEdmM+gSpxwIrlHaPd94CT6ajvcQvPKp1Bq31qRwIiN eSrn6KI/j/B7hXLVR8vxjYCvWQGO+j45bFhXH4DaM3vJyn2Cjmkfu+yEaqTYN4Z+2p B4wU61tjor1Ml2brAU0Sv3w50wKcSeY5p13Xfm2NL6XmfZvblfihHMJpYY+OkhRYie rquvdafeCHE/V3wsoL4udugS4r+W8g7OFByWfxw5AchmpHf2KxJnsCLRpGbrpGCCd1 2AtpIUY02/9oA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9DFEF1801D9 for ; Tue, 11 Feb 2025 16:04:17 +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.0 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 16:04:17 +0000 (UTC) Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-4394c192285so10605715e9.0 for ; Tue, 11 Feb 2025 08:07:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739290019; x=1739894819; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=pL8vyAu11F+S+UI1plAs7j5qoKYWbCfrG6/gFwp76iw=; b=lwd7bJmAUv0763EMBndmSKav/+zNxDGKpWuCAnC/F+O1axJF6cIB4u0vLYeUS3fM6C 9Y1FRoNNSza9Voc4DjjpIN37qnKd7KLJ/b85csuGgwsjjch52bBu9bqELls9JAInw3DW 6XJe9d0kY+VLYgy42SY5EdJCBSujLaNDzgpDeKxS5knjKdJSAtqszUE1WPjzCqjnUKEf wtPlShfwm/ZV8UM6ykdAsrbCGinXOkEZ7IYF5miMKflOAEZ4CRsSFH2Wu1fu82Dfc2na yxvrmZ5DouXI0/IppVgGgo6G2t5Ow8vJyVdJJ8wRwiKUrr2sRzVvz5eavvZ/7RSFYoq8 +jTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739290019; x=1739894819; h=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=pL8vyAu11F+S+UI1plAs7j5qoKYWbCfrG6/gFwp76iw=; b=i4HfWw3kKb6NDtOjTIerMLVVVlzFhRz3Pr2z/7lnDxjEyvdUyFUe8VgwGzPr3Q9HjX bh7jiXDCwopiEG4Nf4GOFmgMgk4ElYZbIQ3LM98JhLlD5Ayoy2etANy/ZAcmz73LrloH SyjUpD9Cg32LmK3hoL/OyTvoGFyWKIcABZGVIvGQtgf1PgTP4VkZ2BIlwuB4+1h4tR8c PlDzF468OpQ2TFjTnrq5atdHGKW5mACSi+L7eA3mbvNHafXu/w4gZSLBlGGsSkG6e7eq qnLnjlxHHOe1hD01/Hg78xZMKpOCkauwTzcMVd4Avth6CIk9NQwOkLjXboBghEsGb0NG Ws8w== X-Forwarded-Encrypted: i=1; AJvYcCVQoRixGcTxsd81ZVdZo4wRLuBrtiK5Jf+pEO/XKTt8W729kcl5O1eRZxpr/+9wXpJdOdjpqXNzr/o=@lists.php.net X-Gm-Message-State: AOJu0YxFNU0zMLthvF1W0Ds2uVQMNuB703edVagKpWLdSNWwyfg1m5xe v8WcAiPbyV0yNneBMXkTTJvaY18DetnEV1M8Hh2bUDDdv/669VDVwt4mCNhEQyGGroN2SaBzcZY DJyO7JB4eHcJ5zpsCNvQlXHis6w== X-Gm-Gg: ASbGnctmRB6tnSorhsZ8DszJnH4ibUTYcugvsheS7xgSsnq4sqquy6D2zVhJ5jykOEA ke8XZM40grXIFL2GGutVo0z0hlFUAL82y5dF5hByALQ36AU+z5zHU8HzIDSY23TuZ+Fqr8oif5w 2Tt9L3fidEQIySQdO8LwTCFps3Rsg= X-Google-Smtp-Source: AGHT+IF2HFg1DzzqVe3de4iUyXGJ/9JZ2pPXh/hMid2ymBHnWNQGAPra42ZVc3FSK12Lr4wry//xAeDLR0pfPrRLeOE= X-Received: by 2002:a05:600c:4f89:b0:439:4bb0:aba0 with SMTP id 5b1f17b1804b1-4394bb0adb6mr47425935e9.8.1739290018525; Tue, 11 Feb 2025 08:06:58 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 11 Feb 2025 19:06:47 +0300 X-Gm-Features: AWEUYZlhWoQUQyLMpfZ4BLu9sYqWJMWdvLf3wKr04od3M943RqcXEIRGJW3G78U Message-ID: Subject: Re: [PHP-DEV] RFC: Not Null Assertion Operator To: Faizan Akram Dar Cc: Mihail Liahimov <91liahim@gmail.com>, PHP internals Content-Type: multipart/alternative; boundary="000000000000b740fa062de003f6" From: udaltsov.valentin@gmail.com (Valentin Udaltsov) --000000000000b740fa062de003f6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Friday, 7 Feb 2025=E2=80=AFat 10:45, Faizan Akram Dar wrote: > > > 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 >> understanding. >> >> Simple examples from Typescript: >> >> let foo =3D ... >> foo!.bar() >> foo!.someProperty.baz() >> >> Examples of potentially using in PHP: >> Without this operator we writing this code: >> >> $foo =3D ... >> >> if ($foo =3D=3D=3D null) { >> throw new FooIsNullException(); >> } >> >> $foo->bar. >> >> 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 syntax. And it >> would be strange if its syntax were different. >> Or we can implement both operator syntaxes: prefix for accessing >> properties, and postfix for use with variables, as in your example. >> >> Nullsafe: >> $foo?->bar; >> Not null assertion: >> $foo!->bar; >> >> If variable bar would be null - php will throw an exception. But now i >> dont know which exception it would be :) >> >> This operator will be useful in cases where a null value in a specific >> place will violate the domain logic. Usually we write either assertions = or >> checks and throw our exceptions for this. But it seems to me that the no= t >> null assertion operator will help avoid writing unnecessary code. The >> problem, of course, will be catching errors. It is not clear how to catc= h >> 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 complain about it, so whats the point of !-> > operator? > https://phpstan.org/r/c9d6e7b3-ac66-4e91-81e8-af0cafdc976c > > > > Static analysis already complain about it That's exactly where `!` is helpful. If at a certain point I am sure that a property must not be null and I want to make that explicit (to both developer and static analyzer), I can use `assert($obj->prop !=3D=3D null)`. However, that requires an extra line of code. `$obj->prop!->x` would be much prettier. --=20 Valentin --000000000000b740fa062de003f6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Friday, 7 Feb 2025=E2=80=AFat 10:45, F= aizan Akram Dar <hello@faizanakr= am.me> wrote:
=

On F= ri, 7 Feb 2025, 08:30 Mihail Liahimov, <91liahim@gmail.com> wrote:
Thank you for yo= ur answer. Now I will give examples for better understanding.

Simple= examples from Typescript:

let foo =3D ...
foo!.bar()
foo!.som= eProperty.baz()

Examples of potentially using in PHP:
Without thi= s operator we writing this code:

$foo =3D ...

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

$f= oo->bar.

With this operator:

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

I think the postfix operator would be illogical in PHP be= cause my operator is similar to the existing nullsafe operator in syntax. A= nd it would be strange if its syntax were different.
Or we can implement= both operator syntaxes: prefix for accessing properties, and postfix for u= se with variables, as in your example.

Nullsafe:
$foo?->bar;Not null assertion:
$foo!->bar;

If variable bar would be nul= l - php will throw an exception. But now i dont know which exception it wou= ld be :)

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


Hi,

I don&= #39;t see the point of this operator, php doesn't allow any operation o= ver null.=C2=A0


It's throwin= g warning (mostly for backwards compatibility), I'd rather have it thro= w error in next php version.=C2=A0

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


<= blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-l= eft:1px solid rgb(204,204,204);padding-left:1ex">

> Static analysis a= lready complain about it

That's exactly where `!` is helpful. If= at a certain point I am sure that a property must not be null and I want= =C2=A0to make=C2=A0that explicit (to both developer and static analyzer), I= can use
`assert($obj->prop !=3D=3D null)`. However, that requ= ires an extra line of code. `$obj->prop!->x` would=C2=A0be much prett= ier.

-- <= br>
Valentin
=
--000000000000b740fa062de003f6--