Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120840 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 66410 invoked from network); 18 Jul 2023 16:47:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jul 2023 16:47:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 35FAD1804B0 for ; Tue, 18 Jul 2023 09:47:36 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f171.google.com (mail-il1-f171.google.com [209.85.166.171]) (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 09:47:35 -0700 (PDT) Received: by mail-il1-f171.google.com with SMTP id e9e14a558f8ab-34637362297so31409045ab.2 for ; Tue, 18 Jul 2023 09:47:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689698855; x=1690303655; h=content-transfer-encoding:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0aHZ06o+lzI5y381ws5/LtQZh4kdmPNxG57lSJlv1hk=; b=TxrmSoyC9bRGUlQOeULi0PJi0oxhetEx3YEpRNEDDbZobG8VnsT97szmAMXMZbkZpQ XL+EwejW/MqKxrUue9ZJPj9GmT1or9xD098yK0JZARTIXm18XFYUi+xK31Qm2cjg4IDU kgdZ/jFrSX2WeCtHeEeed36j18DWSlKvYCGI802cqyHt48Mw3YkqkzmFWWPhF71oa42G bfHOd5FzdLktsujHqfxgwkKzZpXmd6yBg5seePSywriJvTk4H/viJyWRUfGSjFDEo1/C v2F6YWhS/68gV7nfPj/5eAHhfsEeU4Gm9ZJg1H+bFG4dKVDXbZOfZgV+qyhxV6f9bj8X hSyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689698855; x=1690303655; h=content-transfer-encoding:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0aHZ06o+lzI5y381ws5/LtQZh4kdmPNxG57lSJlv1hk=; b=K/XGOnKgs1JjNevioZvFD7XzujnnNZ2ErHRNQit0JF6szzqVB1hL4tBspnpQU7vdRv 1YXInaP4o8Xej622x/Ie3kCMpN2S2WvkYscCkZBx04Cv/uRSoPYhVizc5/FFOL9MvDmK vsWsGoaP4igzrayCE9tbquYXlB3SrZpuINahyhdFWUjw9737EcoqCCS6H72Rztc92Ui4 eJ1YoU/uFBUKioKs9EqdMFjdW7lWT0YdIVoHCDhwVcaYyfVIkk+q7kz9N6+UgyjzeHGD qf9ymuWKuZp/3d7hoG46oD2gL5/bfoH3/pCS7AiWzyGA5CXxcESrpFwpQlueybiIPuvw C5kQ== X-Gm-Message-State: ABy/qLZ2a8yyAlFHWL6RHRAmIQWZIL9qE6CHquEJIC6tbGH/oy1equSB vp0dhq4xzGtj8Cu0b4QlhEdrePKEipC+5fRuoNs= X-Google-Smtp-Source: APBJJlGP3UV3pyh+D3fXuuRBJePhR5fjpA/SALkQJM66CoSz+lrzEMW6OSsvVkD9sYPXLKQIoBPyBJfOZpSSj6NZViY= X-Received: by 2002:a92:c56c:0:b0:348:905d:b73a with SMTP id b12-20020a92c56c000000b00348905db73amr3442604ilj.16.1689698854906; Tue, 18 Jul 2023 09:47:34 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ad5:4204:0:b0:2e2:2af1:79d7 with HTTP; Tue, 18 Jul 2023 09:47:34 -0700 (PDT) In-Reply-To: <51a97131-f64e-4939-9339-24e70873fe32@app.fastmail.com> References: <8055b366-32f1-4d2c-85d0-0cf665e6c8d3@app.fastmail.com> <51a97131-f64e-4939-9339-24e70873fe32@app.fastmail.com> Date: Tue, 18 Jul 2023 18:47:34 +0200 Message-ID: To: Larry Garfield Cc: someniatko , php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] pipes, scalar objects and on? From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2023-07-18 17:13 GMT+02:00, 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 topic >>> to >>> the internals mailing list, but have abandoned this idea, because I fee= l >>> 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 participat= e >>> 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 has >>> 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-flow-control options. PHP at the moment leans heavily on the "null= is > error and build tooling around that" approach, which has its pros (mainly > simplicity) and cons (lack of information). That mostly obviates the nee= d > for a Maybe type, but not entirely. Right now, there is no "null pipelin= e" > option in the language like someniako is describing. > > In some sense, that's also trivial to do in user-space[2], which leads > easily to > > $foo > |> maybe(bar(...)) > |> 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 pipe= s > in general first). > > However, that runs into the related problem that Maybe is... the weaker w= ay > of handling error context. In most cases I prefer a Result/Either type, > 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 metho= d > and dedicated operator). Doing it more natively would require deeper > language hooks. For instance, Rust has the ? suffix on any expression, > which means "if this evaluates to an Error case Result, just return that > 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 null. = So > given $result =3D foo($a)->bar(), if foo() returns an error object then > $result is assigned to that error object and bar() is never called. 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 mixing= 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 case that will > necessarily involve user-space code for many use cases and we'll need to > support that. (Which basically comes down to >>=3D and making monadic cl= asses > 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 > Any comment on a pipe (or piped process) as a first-class value? When you use |> the functions are immediately applied, which might not be what you want. You can lazify it by wrapping it in a lambda, but I think that decreases the value of the pipe concept in itself. One strength is the ability to split the decision about what to glue together from the decision on how to execute it. You kinda get some light-weight metaprogramming ability that way. Well i guess such a pipe-concept is pretty far removed from the simple pipe operator. :) And also already possible with existing OOP concepts, callable classes, and now the (...) syntax. Olle