Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120832 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39440 invoked from network); 18 Jul 2023 12:49:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jul 2023 12:49:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2DF761804DF for ; Tue, 18 Jul 2023 05:49:03 -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,HTML_MESSAGE, 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-vs1-f51.google.com (mail-vs1-f51.google.com [209.85.217.51]) (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 05:48:59 -0700 (PDT) Received: by mail-vs1-f51.google.com with SMTP id ada2fe7eead31-444c5209861so1533840137.2 for ; Tue, 18 Jul 2023 05:48:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689684538; x=1692276538; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=L8TviLWEw4ht7KTPk3dN0FN6UNa8wcIt8B7DoHwGzPI=; b=cr/dBs3DxUn5ltaL8eI83UxeDHUgpQ95X29nWy5WGOcpgWwMCjClo5KtDTbSQlF5Er JgpZTPxKZX8T2FFfO2FFi1SbJ2uvJaaPQxx2cAO6nc9Qjy+UzUgcKMyPtZ27PmXAICpw 308REkWsaWuWXYsA1013ZwwB4hAAhNmA+MakPOkxuQg7nhwJYEV2EkvnyLkUcACsLDQl zopQxxp1rZBV5DJsCOHtfB65CaRQHt7bYyjJXE7dkgGBlTZoJQ3LX7rY+vmSn+uGpxvh 6viwW4KBqemPh98LNMxjQhrxibdABnyog9boH+4J76N6aWqCzY94AMNWdy4Z7xk+8EUz bkEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689684538; x=1692276538; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=L8TviLWEw4ht7KTPk3dN0FN6UNa8wcIt8B7DoHwGzPI=; b=SODmI4z0QuWrQgfShZWx8j8UJmLPBfYzUmbaRUPVzgaFH50L5ZURHKi5FotdiJ8xXL 2+CiGh6/0ISjIj4dJFzpf9YrIvnUTD/1N9W2anntxQVLPIG6NATwNRjdFz4JCnwccyHd ScEgvUV/xnuMjYzxsbjCaNVqIiq9sOAGf7XsI4D1gGtnFklEF78tMcISWbxzyrPpPOQ/ AJ9Ki64bR0cbQZg/h8liKYrboK447yYo0WVt70C+7Q6N5h/IV8Z/4mS0IdtGcV7v29S5 NnRuK1q44NDSScztODQ/SuL3rhWn0lsjFpO4NIat9Tye3NaodCopUH1p2giBZlPdeNUB MHxA== X-Gm-Message-State: ABy/qLaRy1rWvSew3qrIqNg5KfD956+oKikerkh/vGoAAqeT1qy0Yj7Y C/t07+RjxKYya1XE2VgKGvcZrtt+/KDctd12bY3UCkf/HjSvG91V X-Google-Smtp-Source: APBJJlEBTgXEwrRxedX31X/Q7yr5bonZgUSUaY0srVqNaSQRYwaQTW8JffBLoHZWKIFK2H8CvukPZ18fG+mg6V6hW3s= X-Received: by 2002:a05:6102:4b0:b0:443:65cb:1364 with SMTP id r16-20020a05610204b000b0044365cb1364mr1020684vsa.6.1689684538241; Tue, 18 Jul 2023 05:48:58 -0700 (PDT) MIME-Version: 1.0 References: <8055b366-32f1-4d2c-85d0-0cf665e6c8d3@app.fastmail.com> In-Reply-To: <8055b366-32f1-4d2c-85d0-0cf665e6c8d3@app.fastmail.com> Date: Tue, 18 Jul 2023 15:48:41 +0300 Message-ID: To: php internals Content-Type: multipart/alternative; boundary="000000000000af37970600c2562c" Subject: Re: [PHP-DEV] pipes, scalar objects and on? From: someniatko@gmail.com (someniatko) --000000000000af37970600c2562c Content-Type: text/plain; charset="UTF-8" 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)); ``` I'd really like to write such code in a native way: ``` return $repository->getById($idFromHttpRequest) ?|> $serializer->serializeToJson($$) ?|> new Response(status: 200, content: $$) ?? new Response(status: 404); ``` Notice I use the old syntax here, using `$$` as a "piped" intermediate result variable, mostly because I'd like to use arbitrary PHP expressions, instead of piping functions only. I feel this is more natural for the PHP than the more functional-oriented way. I am okay with using functions too like this: ``` return $repository->getById($idFromHttpRequest) ?|> $serializer->serializeToJson(...) ?|> fn (string $json) => new Response(status: 200, content: $json) ?? new Response(status: 404); ``` but that feels less "clean", and also note the inconsistency with `?? new Response(status: 404)` part. Regards, Illia / someniatko --000000000000af37970600c2562c--