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

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

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
October 7, 2022 09:42
Subject:
Re: Named argument syntax (was Re: PSC #049 2022-01-07)
Message ID:
Yz/0f2O/xoZ7o9q/@iabyn.com
On Tue, Oct 04, 2022 at 03:40:59PM +0200, Ovid wrote:
> Reviving an old discussion ...
> 
> On Mon, Jan 31, 2022 at 9:50 PM Dave Mitchell <davem@iabyn.com> wrote:
> 
> > 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');
> >
> 
> Since arrays and hashes are flattened into a list, it seems that someone
> might do sub foo(:@bar) {...} and get surprising results. Either it doesn't
> do what they expect, or it's a syntax error. If named arguments can only
> use scalar values, why not drop the sigil?
> 
>     sub foo (:x, :y) { ... }

I'd expect :@bar to be a syntax error. But note that in a separate
proposal, a \@ary parameter auto-dereferences and aliases a passed array
ref, as in

        sub foo(\@numbers) { say "@numbers" }
        foo([1,2,3]); # prints "1 2 3"

This can be combined separately with named parameters, giving

        sub foo(\:@numbers) { say "@numbers" }
        foo("numbers => [1,2,3]); # prints "1 2 3"

-- 
"Strange women lying in ponds distributing swords is no basis for a system
of government. Supreme executive power derives from a mandate from the
masses, not from some farcical aquatic ceremony."
    -- Dennis, "Monty Python and the Holy Grail"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About