Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114499 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 95320 invoked from network); 17 May 2021 14:41:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 May 2021 14:41:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CAA42180537 for ; Mon, 17 May 2021 07:50:21 -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=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,FREEMAIL_REPLY, HTML_MESSAGE,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-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (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, 17 May 2021 07:50:21 -0700 (PDT) Received: by mail-lj1-f181.google.com with SMTP id w4so7581438ljw.9 for ; Mon, 17 May 2021 07:50:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=F8qR/HPIjcpDmyIyW3hKXHG3jX74nTAqVf7voh95Ygs=; b=qRufIahNm2MBBqPBzN4h891K8ukaglQn7+TINOQFCByFF7sNVGdZFVMfd3GxsSmz0U qlYIa663Fv7PL9z4XsY3lN1Jmj9PImDvVadGKgt7/SCKPYHcsGqCXrfcF/329xiHOi+u /9gFuGkQV/4zOHSC+SLGUOAzAQNi3TyCiY0iDBPQMAsF5mXaaiHfFuxCm7KFxIkhYWKl vTfDI32drPiUTu824zWzzYamRVCOGrUBy2I4r41KZue9q8lzEwSbsnFTbAuxkfmSvr+F sv2tfg8B4AsBHgAn4iYwG0ucwIsxuMkwKMrW5Xshue/+uc832WPRv7nxIkdR/KNSyQMS fL5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=F8qR/HPIjcpDmyIyW3hKXHG3jX74nTAqVf7voh95Ygs=; b=CfCe84hEScV6D7xSPgW3X5Vd7ubB25QBSfc9ps0vDVtodAJRvRZwBlemk/LhfmMOzd pnfABWnTpmC8NorLbyLtaOTWbjSkWYjsdBOYNv9sblxidyq5hwchg3nTPtmCzQNz5sfC PhQhxgWET4uC+sHOYNJ3M00LL+VylMpo/tQk0SKq7If17O1Uru3G/a1pe2HyAArepSSk x5N1IzaWLVBFZ1xhbix41IkrGzjTmiayKmqCCBs7h1zWg+dVNJYUPo/uULTh0pVEySf/ StlxhkYRCKAjaZ9y9yWSr0Gt3qr1J/A+YC4jIujOowcLIGfdEK6eHiPfniNM+ygRswA7 H2PA== X-Gm-Message-State: AOAM53138BH/92Y7u+LhZsTmD8PYlKfKl2M+gPnt16CJjYl0laTvZ2NZ ZYURtkRiozn15g2XoDW/9hKAK9RfUN5ajoQXBQ== X-Google-Smtp-Source: ABdhPJzIspa55Jgo/f62JMqUbsvfpLZYMRtEezoiTCi4+IiQasRrZNnSHSw6hFsywuasWeyhVNt5pQZ4lLgGSOjOT1k= X-Received: by 2002:a05:651c:329:: with SMTP id b9mr49779277ljp.128.1621263018165; Mon, 17 May 2021 07:50:18 -0700 (PDT) MIME-Version: 1.0 References: <1565EB81-57B7-49B0-A47C-342E0088A432@trowski.com> <09B663C3-E21D-432B-BB7F-78312F827C30@newclarity.net> In-Reply-To: <09B663C3-E21D-432B-BB7F-78312F827C30@newclarity.net> Date: Mon, 17 May 2021 16:50:06 +0200 Message-ID: To: Mike Schinkel Cc: Hossein Baghayi , php internals Content-Type: multipart/alternative; boundary="00000000000049093f05c287b685" Subject: Re: [PHP-DEV] [RFC] Partial function application From: guilliam.xavier@gmail.com (Guilliam Xavier) --00000000000049093f05c287b685 Content-Type: text/plain; charset="UTF-8" On Mon, May 17, 2021 at 6:58 AM Mike Schinkel wrote: > > > On May 16, 2021, at 10:43 PM, Hossein Baghayi > wrote: > > > > On Sat, 15 May 2021 at 09:03, Hossein Baghayi > > > wrote: > > > >> Providing ? as a means of placeholder for some arguments and ignoring > the > >> rest could complicate the readability in my opinion. > >> Maybe we should move (?) out of the arguments list as a means of > creating > >> a partial. > >> > >> What I realized is that we need a way of signaling partial function > >> creation. Be it foo(?) or foo(?, ?, ?) or however many ? is added there, > >> which does not convey any meaning and also doesn't produce any sort of > >> error! > >> > >> Say instead of foo(?) we had ?foo(). > >> Since we have named parameters it could help us in providing some of the > >> arguments and deferring the rest. > >> > >> For instance: > >> ``` > >> function foo($x, $y, ...$z) {} > >> > >> ?foo(); // causes a partial > >> ?foo(y: '..'); // also causes a partial > >> ``` > >> > >> This way we wouldn't need to worry about the number of ? added to > >> arguments list. > >> It may also help in avoiding future PSRs in telling us how many ? we > >> should put in there :) > >> > > > > In addition to these, I was thinking of 2 other cases in which changing > the > > current proposal might be helpful. > > > > 1- When there is a parameterless function (an expensive operation maybe). > > 2- When all parameters are passed but the function is not expected to be > > called yet. > > > > In the case of a parameterless function, maybe it is an expensive > function > > call and we need to defer calling it. > > Maybe all we need is to hold a reference to it and pass it around? > > With the current proposal, I do not know if it is possible or not. Since > > there are no parameters defined. > > ``` > > function with_expensive_operations_lurking_inside() {...} > > ```` > > Can we or should we call this function this way: > > ``` > > $ref = with_expensive_operations_lurking_inside(?); > > ``` > > It feels odd having to provide parameters when there is none needed. > > > > > > For the other use case where all parameters are passed but is not > expected > > to be called yet: > > Maybe providing parameters and setting it up is not our responsibility. > > ``` > > function expensive_or_not($a, $b, $c) {...} > > $do_not_get_called_please = expensive_or_not(1, 2, 3, ?); // with an > extra > > parameter as to mark as partial! > > $do_not_get_called_please = expensive_or_not(?, 1, 2, 3); // or maybe > this > > way?! > > ``` > > > > Well, I was thinking that by changing the proposed syntax we could > achieve > > what is proposed and a little bit more. > > Also we wouldn't need to worry about the number of ? needed as arguments. > > Since all we need is to mark the return type as partial (closure) on the > > fly and grab hold of what is passed as arguments. > > > > There are some different syntaxes that come to my mind: > > We could still use ? but outside of the arguments list? > > ``` > > $partial = xyx?(..); > > $partial = ?xyx(..); > > ``` > > or maybe different symbols: > > ``` > > $partial = :xyz(..); > > ``` > > > > We might be able to even cast the return type: > > ``` > > $partial = (?) xyz(..); > > $partial = (partial) xyz(..); > > $partial = (fn) xyz(..); > > ``` > > Casting is another interesting approach that does feel more consistent > with the existing language. > > Since it *is* creating a closure, wouldn't this make the most sense? > > $partial = (closure) abc(); > Ouch! definitely NOT! > $partial = (closure) xyz(?,24); > > -Mike > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > But I agree that these two cases "don't feel quite right": ``` function noParam() {/*...*/} $p = noParam(?); /* looks like there's 1 param, to be passed on call */ ``` ``` function threeParams($a, $b, $c) {/*...*/} $p = threeParams(1, 2, 3, ?); /* looks like there's 4 params, with the last one to be passed on call */ ``` (both would be called as `$p()`, i.e. without any arg). Here we had to add a `?` only for "technical" reasons. More generally I also agree that `?` having two different meanings/behaviors in e.g. `f(?, 2)` [or `f(1, ?, 3)`] vs `f(1, ?)` -- namely: "exactly 1 arg" vs "0+ args" -- is confusing. I think I too would prefer `?` to only mean "exactly 1 arg", and - either have another token for "0+ args" (e.g.: `f(?, 2)` [and `f(1, ?, 3)`] vs `f(1, ...)`, and also `noParam(...)` and `threeParams(1, 2, 3, ...)` ) - or have another syntax like Hossein first suggested (e.g.: `*f(?, 2)` [and `*f(1, ?, 3)`] vs `*f(1)`, and also `*noParam()` and `*threeParams(1, 2, 3)`). Unless there are compelling (or at least convincing) reasons against? Thanks, -- Guilliam Xavier --00000000000049093f05c287b685--