develooper Front page | perl.perl5.porters | Postings from January 2022

Re: Named argument syntax (was Re: PSC #049 2022-01-07)

Thread Previous | Thread Next
Dave Mitchell
January 31, 2022 20:50
Re: Named argument syntax (was Re: PSC #049 2022-01-07)
Message ID:
On Fri, Jan 21, 2022 at 09:38:41PM -0800, Darren Duncan wrote:
> I know this isn't the main point of what you're saying, but I want to
> address the matter of named arguments.
> Your example code using "=>" may have just been representative and not literal.
> But I want to argue that when/if Perl does add native support for named
> parameters/arguments, which I hope it does, I feel that we must use a NEW
> syntax to indicate it, and NOT use "=>".

Just to be clear here. I fully expect that function calls will always
consist of the caller passing a list to the function, and that the
proposed named parameter syntax  is just a way of easily processing
adjacent pairs of values in that list. So in:

    sub foo(:$x, :$y) { ... }

The caller is expected to pass 4 arguments. How the caller does this is up
to the caller. They could do

    foo('x', 1, 'y', '2');
    @args = qw(y 2 x 1); foo(@args);
    foo(y => 2, x => 1);

or even (as I suggest in a separate proposal)

    foo(=$x, =$y)

where '=$VARNAME' is syntactic sugar for


and indeed can be used anywhere, not just in argument lists:

    @point = (=$x, =$y); # short for ('x', $x, 'y', $y);

If anyone expects sub foo(:$x, :$y) {} to be anything other than the
approximate equivalent of the following, please explain how you would make
it differ.

    sub foo {
        croak unless @_ == 4; # actually a bit more subtle than that
        for (0,2) {
            croak unless $_[$_] =~ /^(x|y)$/;
        my %args = @_;
        my ($x, $y) = @args{'x', 'y'};

and in particular whether named args can be called with non-name caller
syntax, to allow modules to upgrade to signatures without affecting

I thought I was wrong once, but I was mistaken.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About