Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126302 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 82CE81A00BC for ; Thu, 6 Feb 2025 08:42:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1738831193; bh=rWebyXXfTDZt/tLQjMwhxFMCuRACbKjTTFBSjZlL/cA=; h=From:Date:Subject:To:From; b=R4M8oQ4KQ7Dakb3EfCdQb2/zz7dbj97D6m4Lx8rt1W5+xjbegDhBRa2gLTXnwiHBL 0yeI7PIzcyDMtpB9CjW1X1n8Zga/37HtgQy+ULLLRXb+3H5ufx1jYsfo/GP3WP+c6C CYx/m8h3NSpY9kT3e5z86pM+6t1j4eCSIw6PLtpbI1qCRRk1S3WopeMs5fQ87QV3qj 9k96uXMQKJWN3fUwDlcMR+1EJgpDPYBgY4xlyV6Obn4qAz1cdcPaAWKzRjIalOCQzf O3Krri0tnteDVZtdbNlfRtWH+tN36F0q4TE8WPhwSXJFP92UwIUPA0pTR0hZcPfD1u pxsTPMz8mrvOg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2E7BD180082 for ; Thu, 6 Feb 2025 08:39:53 +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_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: <91liahim@gmail.com> Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (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, 6 Feb 2025 08:39:52 +0000 (UTC) Received: by mail-yb1-f179.google.com with SMTP id 3f1490d57ef6-e587cca1e47so594902276.0 for ; Thu, 06 Feb 2025 00:42:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738831357; x=1739436157; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=hiLC4oMzfNE4JKC0/uG1Qz5IFHvueETX6QioiETPa44=; b=UpmmW0LhlM6NBUnpvQiw8/oGERCJVHxaZJJ7Kq34+Vb8YLlOBZa/uuoDTtOwXUQFsE eOfAsYoNJW0XMyWWTXWO5rTUXS/iwGyT9NkDKCGocZ4Yw1sIwHbqH7W9itl18+UA7T1Y sOT4pcNaDn0cms0F9n3G7xcMZhpwIlANSyVnMeRlOGr5s5fG+vzUoC8xDtkGX2o3nU8J EY4RffSu8VxPcvGZiDOmnbTEXfzARmSbNzjw4YaduVfmMM0BaMHLa+7S35NcgAHZEZci 1i/5mI/ZVde9zxIp7ne7vpSVKHjcNNEE0PWw3CPkJR2nJmo6+1LiameKgt6HeOmO28Mu 3Deg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738831357; x=1739436157; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=hiLC4oMzfNE4JKC0/uG1Qz5IFHvueETX6QioiETPa44=; b=BtNHvJb5EVkO+XueuKptkK1kfN4w6wDaWUQZAkahpkG5jeLjpjRS9q3Q4cMpCjKtSd zpvv4VKORPM7K1NbrCkszi4vy7eL5BxXLSYwG+o2N5wiz8pE6ctoY5NTDi9sgVKW0VTm qKHgc2RBExXE3AbMCqUUdQF4TZyFeq7YffwvU3FFXlUvNUTKaqdIL6S3L6Jc5ihWsMLn /eHSZ6LCjGXA0ollPRHscHamXE7/vAAQMydyZkmAmKwx+CrpWHUpMSLqvrf+v0hXtfuK DJd/9YZgMH1+WvRxpc/ZZsV1WL9TvuqwoVsCHkv8lEdBlFMy8YL9sQtV5N7jOZ8EKdB/ QqgQ== X-Gm-Message-State: AOJu0YxrgwVOtM/udAMw54JcmEtEng0+LXlxeroTyPPiEgDdMarMd+ph KfWLZGAbuYI3B/rxK8GeqmiqVBNCJ+xh6m/M0ES/ajC400LU7EzdpfqMecgF0ojjQ4GSfgcGb+i eusNCE7HsIY4Y+5h+D+MYiV7s4COE4nYm X-Gm-Gg: ASbGncs3fI1wEg1Dndi08AnhO7WFFOz1FOkjo9soJEzUnZOfILVkc24ia/9Rtn8OpNB OHMwy5miXg3DXjKYnPd3UKbuDw8k6Heltd4S0iE0Yo9woqSsAx6/TZ1DMTmkN7bXXP7UTmG5XEt c= X-Google-Smtp-Source: AGHT+IE//F/ax8g1JrYHvNYFYUnQovu5Yqe1vygsOwwYLaUaadZZ3La2ZWKUjykWZyEeDVeeZM6ShIh6VDK5uIIxY9A= X-Received: by 2002:a05:690c:883:b0:6f7:ac3f:d589 with SMTP id 00721157ae682-6f989f6334cmr49329427b3.36.1738831357348; Thu, 06 Feb 2025 00:42:37 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Thu, 6 Feb 2025 13:42:26 +0500 X-Gm-Features: AWEUYZnMHFsDyGfYMM_Ziiip_1n_mqysLisLpFVOT9Bf6wzxs1L6BeC-tEmNwt4 Message-ID: Subject: [PHP-DEV] RFC: Not Null Assertion Operator To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000611374062d7539f8" From: 91liahim@gmail.com (Mihail Liahimov) --000000000000611374062d7539f8 Content-Type: text/plain; charset="UTF-8" Thank you for your answer. Now I will give examples for better understanding. Simple examples from Typescript: let foo = ... foo!.bar() foo!.someProperty.baz() Examples of potentially using in PHP: Without this operator we writing this code: $foo = ... if ($foo === 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 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. --000000000000611374062d7539f8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thank you for your answer. Now I will give examples for be= tter understanding.

Simple examples from Typescript:

let foo = =3D ...
foo!.bar()
foo!.someProperty.baz()

Examples of potenti= ally using in PHP:
Without this operator we writing this code:

$f= oo =3D ...

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

$foo->bar.

With this operator:
=
$foo!->bar
$foo!->someProperty->method();
$foo!->some= Property->anotherProperty!->method();

I think the postfix oper= ator would be illogical in PHP because my operator is similar to the existi= ng nullsafe operator in syntax. And it would be strange if its syntax were = different.
Or we can implement both operator syntaxes: prefix for access= ing 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 usef= ul 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 exception= s for this. But it seems to me that the not null assertion operator will he= lp 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 th= ink about it.
--000000000000611374062d7539f8--