Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120835 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 45209 invoked from network); 18 Jul 2023 13:18:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jul 2023 13:18:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 20DD418050B for ; Tue, 18 Jul 2023 06:18:33 -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-f180.google.com (mail-il1-f180.google.com [209.85.166.180]) (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 06:18:32 -0700 (PDT) Received: by mail-il1-f180.google.com with SMTP id e9e14a558f8ab-3460815fde5so36182595ab.2 for ; Tue, 18 Jul 2023 06:18:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689686312; x=1690291112; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=sf1d6AC4IdVCL/C7+wQ+oJtHOnX29sdH6a4el2i+Y7g=; b=E+p9L/ISWbUPN56tc3fWbOAkW0yyfc5XpnEUeBlanHMfrjD7EpUJNr0PafQa90TQFa s8/zzeogvrdeCyuBMLdKe11DRk/5v/Yyl47SM/sks0/6fwhZtZxyt/OKPK0g4+gKoRrY 8+zs/swwWNb68m0HOPGm5QXKwZCR3vO0LN1K9IRx/p8mQ7jSTlyUkr2DwgM+aYx8tHr7 yHsTvM/+u15bxpxkI0gQFp7w9DTcEaCEk/sUlnzdrqfp3toqKj8m/6zYjkprvu7MHTNy oaup6tzQo3TnVAQZzhXA/+/6r1KgL9/C4rCT9u5aMSJuZUyjSmIup3O1HKnsa/9yKT4S Z7bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689686312; x=1690291112; h=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=sf1d6AC4IdVCL/C7+wQ+oJtHOnX29sdH6a4el2i+Y7g=; b=Q3sRlJiY0jFTOKgr3/XvY2aUZPLF2caLbgaY+kTyz5FHeHZ7xXLjDu3y860NU0tusz yS3AS41TnPKdpx/lT7fLWKCt0v8X3QpTs0ya3VKg9H2Ekzh/lXJRHG/FUgCiF6dzUxtv ZrlOpzep5gsW3WG36u58QxQfOsytbg9r9Get8aM3M9DrgwlfGOsyBr3xer8gyHsgsc5j q6xa06Hj/zEd6o3HxBVMIM1GzTsyWD6sSFI4Tt84Vf5YDQN8s5sRvkfi/T5bqqM/3JkN uUWd6KVjRnASqz1d4GR+JF1NFKK2MrhAbuJw2nqLSei7XNpqMIcARmf6vsEEVbOr8BSj KXKA== X-Gm-Message-State: ABy/qLakVjjyFOh/MAJEGNkbJwWqEz9+cIKZYsoOwQ3413sSWeG9KLtS zf6qBI5x3DampoZyzelu5pRYaa3IiqB5PxAJ53M= X-Google-Smtp-Source: APBJJlGzHbu2N3nuWCrieLnRYwxKBul6ppTAqusxqW5ETkLSqLIVv8xaPjrfpN9PEGdCawIDk9hycLeNpTOjx0umuOQ= X-Received: by 2002:a92:c70a:0:b0:348:8541:d969 with SMTP id a10-20020a92c70a000000b003488541d969mr2542616ilp.12.1689686311647; Tue, 18 Jul 2023 06:18:31 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ad5:4204:0:b0:2e2:2af1:79d7 with HTTP; Tue, 18 Jul 2023 06:18:31 -0700 (PDT) In-Reply-To: References: <8055b366-32f1-4d2c-85d0-0cf665e6c8d3@app.fastmail.com> Date: Tue, 18 Jul 2023 15:18:31 +0200 Message-ID: To: someniatko Cc: php internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] pipes, scalar objects and on? From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 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 feel 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 participate 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 also maintain > a similar library https://packagist.org/packages/someniatko/result-type > which fixes some shortcomings of the original one related to the static > analysis, but this is another story. Basically what the stats tell us is > that such stuff is popular among the PHP community. > > In my eyes, it is actually semantically equivalent to the nullable PHP > types: `?Type`. And some operations provided by the lib, are actually > covered by PHP itself, which has multiple null-friendly operators: > `$option->getOrElse($defaultVal)` --> `$nullable ?? $defaultVal` > `$option->isEmpty()` --> `$nullable === null` > `$option->getOrThrow(new \Exception('blah'))` --> `$nullable ?? throw new > \Exception('blah')` > > I'd like to use the arguably "more idiomatic" native PHP nullables, rather > than a foreign-feeling userspace construct, if they were more convenient. > > But there is a very important operation, `map()`, which is unfortunately > not covered by the native PHP, which is `Option::map()`, and here is a > real-world example: > ``` > return $repository->getById($idFromHttpRequest) > ->map($serializer->serializeToJson(...)) // only executes when the > Option is Some, not None > ->map(fn (string $json) => new Response(status: 200, content: $json)) > ->getOrElse(new Response(status: 404)); > ``` Ehm, wouldn't that be the same as a Pipe class that's configured to stop on null? public function getThing($id) { return new Pipe( $this->getData(...), $this->serializeData(...), $this->mapToResponse(...) ) ->stopOnEmpty() ->from($id) ->run(); } Wait, are you using map() for arrays or not? Looks like not. Olle