Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115198 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44834 invoked from network); 28 Jun 2021 22:26:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Jun 2021 22:26:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id ED01B1804C3 for ; Mon, 28 Jun 2021 15:46:14 -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 autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.175]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 28 Jun 2021 15:46:14 -0700 (PDT) Received: by mail-lj1-f175.google.com with SMTP id q4so19739653ljp.13 for ; Mon, 28 Jun 2021 15:46:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=5PzQBlNBquUterlLIdrdaMQV8XcGJZdehTvt5Gu7OXQ=; b=jm1SDyAsRR7RxMGzaZuj1N/lwNAAKrqdH5Jzm/vDJLOx3wxMEvMM8QYRoBrCqmwR1P sjv1Lj83pr3Q2u9WBMC79QdAimwmH9tLR8ijd7JqrCCXn21GKBp0jT/d90TdzFd0vHk0 qrwXE0LMUI9Zu4wQD9xIN0oHiaUQaPlY3orkBGN2vMezLPsb8QM1ELwDMtWYbqGyalfX 7pusTpORKSU7+xRrZ0w4ajOX8WKsQDsulaJkdnSjvPZYs2nBZ66MN6ydiIpiFTUFplHF tO8MP6dq+9V93cKj8DFZreOFc7i/iIJB2opNDvwSMr0OcsqgFy7WmiHWs4Q4BmyMrtpu sccw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=5PzQBlNBquUterlLIdrdaMQV8XcGJZdehTvt5Gu7OXQ=; b=L9f+3Aiq0hyeMR9JlolLVtMD8tKm21U+C3N4l3zIX2KMG+6aPrANUNr6LwvRS3rdTh b6jTVv65Bp9sWEUrYNRHBqHJo2BisvbikAjnb19kouzwJiMMsbeA3C50CjEZJfgWfDib wYRINalcDeQu+2TvAKMsKnlNI8rJBZBmFhQmj2EhpufC1pXd7Iwmi62j1PSfUY/aqA0w IprVlUJvm0jPf2mB3/HEyHtv5TenuJyeZJYBTntPxsMrA2Kad6RhOM+5pHW7oz/Py33V mfd88pdi4NgaExXCmRY16n8IcSax98/sS393aO9bhFgw6RGd5qoFX5HE6Dbaj1XDTG1Z FUdg== X-Gm-Message-State: AOAM531GyJrZP0GLHLcPD9h2LECTfSxX8lDDQgYLEgm4Y4Fw1alUaa+D DU7/9pmuBj0Rz/Nf3hic9Pc6tEEFk7tbyL+73Yo= X-Google-Smtp-Source: ABdhPJz62De5JkU8ijkfDvbLKqWp3e58qCU+ZgdgCYO0fpfVB/G9Q81nXO2mafaaXeU94zl4MoJQsebzsakxcGCOFJo= X-Received: by 2002:a2e:b0c6:: with SMTP id g6mr1367252ljl.470.1624920372084; Mon, 28 Jun 2021 15:46:12 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ab3:7506:0:0:0:0:0 with HTTP; Mon, 28 Jun 2021 15:46:11 -0700 (PDT) In-Reply-To: References: Date: Tue, 29 Jun 2021 00:46:11 +0200 Message-ID: To: Rowan Tommins Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] Pipe Operator, take 2 From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2021-06-29 0:06 GMT+02:00, Rowan Tommins : > On 28/06/2021 21:28, Olle H=C3=A4rstedt wrote: >> Sorry for hijacking the thread, but are there no other alternatives, >> really? Just brainstorming: >> >> 1) Setting to silence the warning. > > > Just to reiterate: in PHP 8.0, an undefined constant is not a warning, > it's an error. My apologies to code golfers: you can no longer save two > bytes by leaving the quote marks off your strings. > > However, that's not really the problem. The problem is when there *is* a > constant with that name: > > const foo=3D'strtoupper'; > function foo($x) { return 'test'; } > $input =3D ['hello', 'world']; > var_dump( array_map(foo, $input) ); // this runs strtoupper(), not foo() > > This doesn't work even in old versions of PHP where undefined constants > fell back to strings. > > Even in cases where this trick did work in older versions of PHP, it was > just a variant of option one, "use our current ugly callable syntax" - > it saved you those two bytes, but it didn't actually provide you a > function reference. > > Unifying the constant and function name tables would not be to just a > different way of writing strings, it would let array_map(foo, $input) > actually check that function foo existed, and represent callables as a > proper type. Meanwhile, to let array_map($object->foo, $input) do the > same thing (instead of the current [$object, 'foo'] syntax) you also > need to unify the method and property tables, which is probably even > harder. > > > >> 2) Setting to silence the warning IF and only if argument expects a >> callable, like in array_map (won't work with a pipe() function, >> though, since pipe() would take any number of arguments and that can't >> be typed) > > > In the general case, this is a non-starter: the parameters need to be > parsed from the source code before function definitions are even known. > You could special-case a handful of built-in functions, but that would > be extremely clunky. > > > >> 3) Silence the warning if you type-case explicitly, like in >> `pipe($start, (callable) htmlentities);`. Another alternative is >> `(fn)` as a type-cast. Not much better than `pipe($start, >> htmlentities(?))`, I guess. Just different style. > > > This falls into my third option: "add a dedicated callable syntax". That > could be something that looks a bit like PFA but isn't, like Nikita's > RFC; or something that looks a bit like a type cast; or any number of > other ideas which have been brainstormed for about as long as I've been > programming PHP. > > >> 4) Silence the warning ONLY when it's the right-hand argument to pipe >> operator, like `$start |> str_len`. > > > Re-wording this as "interpret the unquoted word as a function reference > rather than a constant when...", this one is at least plausible. In > practice, though, a lot of functions don't take only one parameter, so > the previous pipe proposal included a placeholder for which argument you > wanted to "pipe into". At which point you don't need to special case > keywords, because you can just write this: > > $start |> str_len($$) True. I wish we had numbers for how common the case is when you DON'T want to pipe into the first argument. If it's rare enough, it might be worth it to let the rare cases be dealt with by writing manual lambdas. So allow some boilerplate. Another alternative would be to add both '?' and parsing unquoted string as function reference together with pipe operator, but start with the simple one (whichever that is, I wouldn't know). Or start with the one that has chance of being accepted by the voters. :) > Regards, > > -- > Rowan Tommins > [IMSoP] Thanks for your explanations! Olle