Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126326 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 0AB231A00BC for ; Fri, 7 Feb 2025 11:17:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1738926863; bh=FIDISjEVEkuzSNkCkNtGVC51ES5CxswzN6zdXqY3xSM=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=OF43351/NI3zWwOmyDuLDR+3O1BIdTkdGeSLxijGIpS9gVD0cVNasgHhia6LZ8Zhu idILYxVMXrgUmBrG2jHKtC6rZmBy9OOjL+PPALALBTSDcsWfyNxdmCMO0gU7FWe/Uc PL9I3SOTIhsL+Z9sWhxPEiLNzRSbJ/ru6WBPW4BbpnLHjNlP7QxP3JuQvnFZTvMRb1 Wo+81mmZ0KFzCwPngMWVi6V3gdgiPVNlKqNR10rKA5DFMm+VW0hygE0K5iP4c52fr0 hw4EcPjPxOtn7mpje1kXNyl0ZGbiwioIVpSbRAHw4Eegi0w3iR30kT0cB8Je7nFIbn +hxBISTn47apw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C418718003B for ; Fri, 7 Feb 2025 11:14:22 +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-a2-smtp.messagingengine.com (fhigh-a2-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 ; Fri, 7 Feb 2025 11:14:22 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id BB80A114011F; Fri, 7 Feb 2025 06:17:06 -0500 (EST) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-01.internal (MEProxy); Fri, 07 Feb 2025 06:17:06 -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=1738927026; x= 1739013426; bh=TeYA3/AcWYFeQNbeG+urYHjqfWgODX35nP2weSQRfwM=; b=Y S46L5m5JehkbpmGEy9E8gxc2naUZd9kv+WeB8c80wMAkXc/vvk2q1IHG/sdHyc6/ pzpDc5XFkQ4sp1tALAsvsnIMAJVJQ+i7GAOMuP55jcPoE0Mwgq4NNlpX5bbfXbjL HiJylAgCrgty579L53X3QZWe9wT00eDpBvOUunuNQERgrK/o7WxtEyfSxU4v3ZgA pSEmGBZa0hZkKQPB7HX7GomeeZicWRO8ZTot054qkPsmeLO+5NZkhQPn2h/R647g sgBxTk/bQE0dXqOwHhdz9U8vLIuFj/lqYH9EP8yTRNZh9sBdNrhQlr7mx4eBrGAA xm/o9wkfCoNWp5/g00UqQ== 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= 1738927026; x=1739013426; bh=TeYA3/AcWYFeQNbeG+urYHjqfWgODX35nP2 weSQRfwM=; b=fAzbay8ImeJeJo3U297Gn39kUGXYD0kN8Y34voCqoSpRBiaJYZA MKgptmDav4LnGWSpfdecqng0ef7htnMFtG0QCaugqNjf3VR5DbHLKo5gzVIVwYgT vYpkh9XFa7WJ0NZdzBJ37srqMD3V8Xo4T+kORd3FphtjzDYJKippMBWAa2/UCNxI FhHD7396QVUaywmRWq8h57kwhfvU9mKGbx747iTGTRUncClXh40dSb/RoeCzGhV6 ZUGa3fbYNoGdgJ3BIbS6BZHFsourxZTadxHuAaKZW57YEaMC4cEmWyCZ5xX6MKNT /b/zeArovp4cJdlu1jsM8jSC7Np0gmGxY8w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvledugecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepofggff fhvfevkfgjfhfutgesrgdtreerredtjeenucfhrhhomhepfdftohgsucfnrghnuggvrhhs fdcuoehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtffrrghtthgvrhhnpeeiue ethedvvdefjefhgfeiheelheehtdfhfeekjefflefgvedvkeduteejjedttdenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgssegsohhtth hlvggurdgtohguvghspdhnsggprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdp rhgtphhtthhopeeludhlihgrhhhimhesghhmrghilhdrtghomhdprhgtphhtthhopehilh ihrghorhhlohhvuddvgeesghhmrghilhdrtghomhdprhgtphhtthhopehinhhtvghrnhgr lhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 6EC63780069; Fri, 7 Feb 2025 06:17:06 -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: Fri, 07 Feb 2025 12:16:40 +0100 To: MrMeshok , "Mihail Liahimov" <91liahim@gmail.com> Cc: internals@lists.php.net Message-ID: <81ba09d5-d8fa-4c26-8ef0-d4b7bf0de52a@app.fastmail.com> In-Reply-To: References: Subject: Re: [PHP-DEV] RFC: Not Null Assertion Operator Content-Type: multipart/alternative; boundary=783993af0b774812b72fbebc5ad3e378 From: rob@bottled.codes ("Rob Landers") --783993af0b774812b72fbebc5ad3e378 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Fri, Feb 7, 2025, at 10:21, MrMeshok wrote: > > > > 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(); >=20 > I see where you're coming from, in my code I had to deal with a lot of > APIs with models which have nullable properties, but they shouldn't be > null if some conditions are met. > So I work with them in this way > ```php > if ($response->code =3D=3D=3D ResponseCode::Success) { > // order and quantity are nullable > return $response->data->order->quantity ?? throw new > UnexpectedResponseException($response); >=20 > // With method calls you would need to add nullsafe operators > // order and date are nullable > return $response->data->order?->date?->format('Y-m-d') ?? throw > new UnexpectedResponseException($response); > } > ``` > UnexpectedResponseException is just an exception that keeps faulty > response, so I can catch it and act on it, or it just gets logged and > I can show producers of API that they have a problem. >=20 > I see a point in !-> operator if it would work just like that. Throws > a specific error that has an object that triggered this error. >=20 Hello, I feel like that is more of an architecture problem than a programming o= ne though. Instead of constantly checking if the conditions are right, u= se php's type system instead, so it would read more like: if ($response instanceof SuccessfulResponse) { return $response->data->order->quantity; } throw new UnexpectedResponseException($response); I'd argue that having an "undefined" aka "null" quantity in a model at a= ll is rather strange as that doesn't seem to represent any possible real= ity outside of a computer (unless you are dealing with Schrodinger's cat= or other quantum effects). =E2=80=94 Rob --783993af0b774812b72fbebc5ad3e378 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Fri, Feb 7, = 2025, at 10:21, MrMeshok wrote:
>
> Examples of potentially usi= ng in PHP:
> Without this operator we writing this code= :
>
> $foo =3D ...
><= br>
> if ($foo =3D=3D=3D null) {
> &n= bsp;   throw new FooIsNullException();
> }
>
> $foo->bar.
>
=
> With this operator:
>
>= ; $foo!->bar
> $foo!->someProperty->method();<= br>
> $foo!->someProperty->anotherProperty!->metho= d();

I see where you're coming from, in my = code I had to deal with a lot of
APIs with models which ha= ve nullable properties, but they shouldn't be
null if some= conditions are met.
So I work with them in this way
```php
if ($response->code =3D=3D=3D ResponseC= ode::Success) {
    // order and quantity a= re nullable
    return $response->data-&= gt;order->quantity ?? throw new
UnexpectedResponseExcep= tion($response);

    // With= method calls you would need to add nullsafe operators
&nb= sp;   // order and date are nullable
  = ;  return $response->data->order?->date?->format('Y-m-d= ') ?? throw
new UnexpectedResponseException($response);
}
```
UnexpectedResponseExceptio= n is just an exception that keeps faulty
response, so I ca= n catch it and act on it, or it just gets logged and
I can= show producers of API that they have a problem.

I see a point in !-> operator if it would work just like that. = Throws
a specific error that has an object that triggered = this error.


Hel= lo,

I feel like that is more of an architec= ture problem than a programming one though. Instead of constantly checki= ng if the conditions are right, use php's type system instead, so it wou= ld read more like:

if ($response instanceof= SuccessfulResponse) {
  return $response->data-&g= t;order->quantity;
}

throw= new UnexpectedResponseException($response);

I'd argue that having an "undefined" aka "null" quantity in a model at= all is rather strange as that doesn't seem to represent any possible re= ality outside of a computer (unless you are dealing with Schrodinger's c= at or other quantum effects).

=E2=80=94 Rob
--783993af0b774812b72fbebc5ad3e378--