On Tue, Mar 14, 2023 at 1:57 PM Rowan Tommins <rowan.coll...@gmail.com> wrote:
>
> On Tue, 14 Mar 2023 at 10:39, Bob Weinand <bobw...@hotmail.com> wrote:
>
> > Hey Rowan,
> >
> > do we actually need *positional* partial application, after a ... token?
> >
> > Would it not be enough, to simply forbid positional arguments after a ...
> > and just allow named arguments? These already have well defined position
> > independent semantics.
> >
> > There may be some desire for a single argument placeholder later on, but
> > this can be introduced later, separately.
> >
>
>
> Yes, named parameters would certainly be better than left-to-right only.
> It's definitely less elegant, though, and given that PFA is largely
> short-hand for a short closure anyway, I think conciseness is quite an
> important aim.
>
> To take a couple of the above examples, and compare existing short closure,
> fully positional PFA, and named-after-placeholder PFA:
>
> $isLogger = fn($object) => is_subclass_of($object, LoggerInterface::class,
> false);
> $isLogger = is_subclass_of(?, LoggerInterface::class, false);
> $isLogger = is_subclass_of(..., class: LoggerInterface::class,
> allow_string: false);
>
> $priceFormatter = fn(float $num) => number_format($num, 2, ',', '.');
> $priceFormatter = number_format(?, 2, ',', '.');
> $priceFormatter = number_format(..., decimals: 2, decimal_separator: ',',
> thousands_separator: '.');
>
> Arguably the named param version is more explicit, but in some cases it's
> significantly longer than manually defining a closure, whereas fully
> positional PFA is always shorter.
>
> Regards,
> --
> Rowan Tommins
> [IMSoP]

Something I was partial to (pun slightly intended), when thinking
about it last summer was to put named parameters with dots following,
like this:

$isLogger = is_subclass_of(object_or_class..., LoggerInterface::class, false);
// or, since this is a beginning/end partial, this is the same:
$isLogger = is_subclass_of(..., LoggerInterface::class, false);

$isLogger(object_or_class: $myClass);
// or
$isLogger($myClass);

$priceFormatter = number_format(num..., 2, ',', '.');

At least, that was what I was going to propose, according to my notes.
Looking at it 8 months later, I still kinda like it.

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

Reply via email to