Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120837 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 56030 invoked from network); 18 Jul 2023 15:14:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jul 2023 15:14:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 186C31804F2 for ; Tue, 18 Jul 2023 08:14:01 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-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,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 18 Jul 2023 08:14:00 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id D4945320095E; Tue, 18 Jul 2023 11:13:58 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Tue, 18 Jul 2023 11:13:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm3; t=1689693238; x=1689779638; bh=FaI1I1IUl9 T37MdQjCP9PpaVTN0ZlLP4X7jdMNSVF8Q=; b=N0QYgh9iX9i4KPn2zZwqH2uihR VmbQe3zXf4Y27UE64/wo+G3k4PHBIGQIsWqSFUppbwRZN5nowH9ky0NES1Yjf9Kg nCxbGRvIo7H8DKhsRCIDP4IHXZGir+C2ORX/WRTBGQsZC2I/JcjHGbEg07kD01hq m6gs5q+82ayKdYymHsaXcQLndIzgi36geSmuzXfD2TWhGX8JXx/D+NNvXQN6IWUc 7x5cEH/AVWaXgs7elp8eZY3eyprzJi+JQEGGMa5gYxzZ3jOZE1GgBjOaHEdkEDSU kvyqxq42jwcb3PjLVT3E1Hdza4ZDpBF/KhwUdT5lhG37ugxdflNKmcok2Zpg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :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:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1689693238; x=1689779638; bh=FaI1I1IUl9T37MdQjCP9PpaVTN0ZlLP4X7j dMNSVF8Q=; b=WQ4ej4etpnZRH+/2q5ai/+OSwn9otGkGbs/goeKw0H84XgdzsZm 7Q6M2ze3MLgwuS2WZ1/wt6bkHcnuT6YI4Qt5zNEksLv11q2JueoK7dN888ZtBn2D 9SjWfENQPiQ07fsGmKfddPG9VEXdgufhrRL1lw4lPnoSHHB9JfAAks+uBX8UVnR0 KOUoUBy+0CWRoNnZBc64vRapCK6RMc6GLWxoOzNRu007xvkG8MPQeyRnD1zk7fJV SH9F60oi7zhhoxRn3p+0yBzLvCGjaES+HMy1EJA0hN/KsXRvHEzTcht703gshwdL ZQ2YgjEYThIKHZeI1VFL2NBH+agjuzxwGEQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrgeeggdekfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvvefutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepvefgtdefgfeigffhtdelteeggeeuudegfeetleej ieehledvtdduvdejudfhueeunecuffhomhgrihhnpehgihhthhhusgdrtghomhdpphgvrg hkugdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhr ohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 073F31700096; Tue, 18 Jul 2023 11:13:57 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-531-gfdfa13a06d-fm-20230703.001-gfdfa13a0 Mime-Version: 1.0 Message-ID: <51a97131-f64e-4939-9339-24e70873fe32@app.fastmail.com> In-Reply-To: References: <8055b366-32f1-4d2c-85d0-0cf665e6c8d3@app.fastmail.com> Date: Tue, 18 Jul 2023 15:13:22 +0000 To: =?UTF-8?Q?Olle_H=C3=A4rstedt?= , someniatko Cc: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] pipes, scalar objects and on? From: larry@garfieldtech.com ("Larry Garfield") On Tue, Jul 18, 2023, at 1:05 PM, Olle H=C3=A4rstedt wrote: > 2023-07-18 14:48 GMT+02:00, someniatko : >> I am glad this topic arose! I was also planning to write on this topi= c to >> the internals mailing list, but have abandoned this idea, because I f= eel it >> might be inconvenient for the real active PHP developers on the list = to >> receive too many emails from the people which don't actively particip= ate in >> the development itself. >> >> My interest in the pipe operator might seem a little non-standard - >> basically what I'd really want to see is a **nullable** pipe operator! >> >> There is a popular library github.com/schmittjoh/php-option, which ha= s 250 >> MILLION installations. Basically what it provides is a class-wrapper = of a >> value of lack thereof: it's either `Some` or `None`. > > I'd just like to mention that the Option type comes from FP and it's > not necessarily needed in PHP-land where we have flow-sensitive type > checkers like Psalm (a nullable type `?int` or such is refined to just > `int` after a null check). Flow-sensitive type-checking was not > invented when the Option type was first created, so adding it to PHP > is basically stepping back in time, from my point of view. As I've written before[1], there's a lot of overlap in different error-f= low-control options. PHP at the moment leans heavily on the "null is er= ror and build tooling around that" approach, which has its pros (mainly = simplicity) and cons (lack of information). That mostly obviates the ne= ed for a Maybe type, but not entirely. Right now, there is no "null pip= eline" option in the language like someniako is describing. In some sense, that's also trivial to do in user-space[2], which leads e= asily to $foo=20 |> maybe(bar(...))=20 |> maybe(baz(...)) |> maybe(beep(...)); Which isn't terrible, but also isn't ideal. I don't think I'd oppose a = nullsafe pipe if there's interest (though again, we need interest in pip= es in general first). However, that runs into the related problem that Maybe is... the weaker = way of handling error context. In most cases I prefer a Result/Either t= ype, which doesn't map cleanly to null handling. That could be done as = a user-space monad (as above, with just a class and method or a magic me= thod and dedicated operator). Doing it more natively would require deep= er language hooks. For instance, Rust has the ? suffix on any expressio= n, which means "if this evaluates to an Error case Result, just return t= hat Result." That wouldn't fit well in PHP, though. As I noted in [1], one option would be to allow an object to flag itself= as an error object, and then null-check operators would treat that as n= ull. So given $result =3D foo($a)->bar(), if foo() returns an error obj= ect then $result is assigned to that error object and bar() is never cal= led. I'm still not sure if I like that approach myself, but am open to = hear what others think. To get back on topic, I do think a pipe operator and concat operator are= valuable in their own right, and have ample use cases even without mixi= ng in monads or nullables or things like that. We should add them. We = can consider adding more robust branching options as well, (be that >>=3D= , ?|>, or something else), but be aware that beyond the very trivial cas= e that will necessarily involve user-space code for many use cases and w= e'll need to support that. (Which basically comes down to >>=3D and mak= ing monadic classes easier to work with, but also runs into the generics= problem, and that's its own mess.) [1] https://peakd.com/hive-168588/@crell/much-ado-about-null [2] https://github.com/Crell/fp/blob/master/src/composition.php#L39 --Larry Garfield