Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:114474
Return-Path: <marandall@php.net>
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 20880 invoked from network); 14 May 2021 21:09:41 -0000
Received: from unknown (HELO localhost.localdomain) (76.75.200.58)
  by pb1.pair.com with SMTP; 14 May 2021 21:09:41 -0000
To: internals@lists.php.net
References: <f3d20496-c560-42b7-9a0d-8672b3cafa7d@www.fastmail.com>
Date: Fri, 14 May 2021 22:18:17 +0100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
 Thunderbird/78.10.1
MIME-Version: 1.0
In-Reply-To: <f3d20496-c560-42b7-9a0d-8672b3cafa7d@www.fastmail.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
X-Posted-By: 87.81.129.66
Subject: Re: [RFC] Partial function application
From: marandall@php.net (Mark Randall)
Message-ID: <php.internals-114474@news.php.net>

On 25/04/2021 20:25, Larry Garfield wrote:
> Discuss.

There has been intense discussion in R11 about how the ? token applies 
to missing or variadic arguments.

The root of the concern is that in the current proposal, ? applies as 
either a placeholder for a single replacement, or 0-or-more varadic 
arguments, depending on its position and the number of arguments.

I and several others believe that this feature would be better served by 
restricting ? to representing a single argument, and using ...? to 
represent "anything else" including an empty set.


## To illustrate the scope for confusion:

foo(?) looks like it requires one argument, when in reality it could 
accept none, 1, or a hundred.

foo(1, ?) looks like it also accepts one argument, but it could accept 
0, or 100, depending on the function it is wrapping.

foo(?, 1) definitely requires at least one argument, but could accept 
more depending on what it's wrapping.


## Suggestion

Based on discussions in R11 there seems to be broad agreement that ? 
should represent a single argument, and ...? should represent everything 
else (including no arguments).

Thus:

foo(...?) - Partial of foo with all the arguments copied over.

foo(?, 1, ..?) - 1 required, then 1 fixed, 0 or more others are copied over.

foo(1, ...?) - Fix the first parameter and copy over the rest.


This can be seen as a mirror to the ... operator that accepts a variable 
number of arguments.

function foo($a, ...$b) {  }

Equally it matches the syntax for unpacking into arguments:

$a = foo($a, ...$b);

Passing more arguments than the partial defines would result in an 
argument count error.

--
Mark Randall