> On May 15, 2021, at 4:20 AM, Rowan Tommins <rowan.coll...@gmail.com> wrote:
> 
> On 15 May 2021 00:09:41 BST, Paul Crovella <paul.crove...@gmail.com> wrote:
>> I think this highlights where the misunderstanding of this feature is.
> 
> 
> I think the fact that there is so much confusion highlights why it is worth 
> considering different designs.

Exactly this.

I have been debating whether to comment on this thread, but seeing Rowen's 
comments mean I am not alone in my opinion so here goes.

I too have been bothered with the inconsistent usage of the single question 
mark ('?').  While it makes perfect sense once it is explained, it is possibly 
not intuitive for those who have learned how to use a command line shell where 
a single question mark indicates a single "thing" (where "thing" is a single 
character in the shell.)  

Using it in PHP to mean "one, or more" seems like it would cause needless 
confusion given that intuition derived from command line experience might lead 
developers to be confused.  Better to use something which would not be likely 
to lead developers astray I would think.

> 
>> Partial application is about binding arguments. ? isn't an argument,
>> it's an argument placeholder. It does two things: signals to create a
>> closure wrapping the function rather than calling it immediately, and
>> holds a position in the argument list so that an argument further to
>> the right can be fixed (bound) at that time.
> 
> 
> This is not a correct description of the current syntax. Currently, "?" 
> represents a *required* argument in the argument list, but *only* if there is 
> a fixed value to its right. If it appears at the end of the argument list, or 
> with only other ? tokens to its right, it *only* signals that a partial 
> should be created, and doesn't create a required argument, even though it 
> looks the same.
> 
> foo(?, 42) creates a closure with one required argument; foo(42, ?) creates a 
> closure with no required arguments
> 
>> Requiring additional trailing argument placeholders or adding an
>> additional token `...?` unnecessarily complicates things, burdens the
>> user, and only serves to further promote misunderstanding.
> 
> 
> On the contrary, saying that "?" always means exactly one argument massively 
> simplifies the description of the feature. Why persist with a version of the 
> syntax that is so easy to misunderstand when we have a really simple fix 
> available?
> 
> I acknowledge the need for a syntax to say "accept zero or more further 
> arguments", but this doesn't need to overload the syntax for "create a 
> required argument here".
> 
> If the suggestion of ...? is too long, we could look at other options like 
> ... or ?? The syntax for "just make a closure and pass all arguments through" 
> would then be "foo(...)" or "foo(??)".

Yes!  

Since we need a sigil to indicate a function be wrapped in a closure I was 
actually planning to propose `??` so I was pleasantly surprised to see Rowan's 
suggestion.  Ellipses would work too, but as I think `??` would be easier to 
see in dense code, so I would prefer them.

Which gives us::

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

$x = foo(??);                           // Wrap foo() in a closure which 
expects 3 parameters
$x = foo(?, 24);                        // Wrap foo() in a closure which 
expects 2 parameters with `24` binding to `$b`
$x = foo(?, 24, ??);                    // Same as foo(?, 24);


> There is a *separate* question of whether arguments that weren't *required* 
> are passed along anyway. I'm less sure there's a right answer there, and 
> would be happy with a version where foo(?, 42) and foo(?, 42, ...) were 
> equivalent - that is, the trailing "all other arguments" token would only be 
> needed if there wasn't already a placeholder.

Ditto, but with `??` preferred.

Said another way, I am arguing exactly what Rowan arged except that I have a 
preference for one sigil over the other.

> 
-Mike

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to