Front page | perl.perl5.porters |
Postings from December 2019
Re: Named Parameters
From: Zefram via perl5-porters
December 2, 2019 13:45
Re: Named Parameters
Message ID: firstname.lastname@example.org
Dave Mitchell wrote:
> :$name1, # named parameter, consumes ("name1", value) arg pair
I'm dubious about this being something that the core should do. Named
parameters are not a feature of Perl 5 sub calling per se, the way they
are built into Perl 6. Seems like something that a plugin on CPAN should
do (once there's a signature plugin mechanism). I worry about a repeat
of the smartmatch debacle, where the Perl 5 core inhaled too big a dose
of a specific person's rough translation of a bunch of Perl 6 semantics.
Especially with the new semantics being available only in this one way
through this quite specific feature, rather than being a new paradigm
that shows up in several places.
If it is implemented (especially if in the core), then I'd want there to
be some way to separate the naming of the parameter in the argument list
(part of the sub's API) from the naming of the lexical variable (part
of the sub's implementation). You didn't mention any such mechanism.
Although it's good to have some shorthand for the common case of the names
coinciding, it really ought to be possible to give the lexical variable
a different name, or indeed no name to ignore it. If you go with the
Perl 6 ":$name" syntax, then the Perl 6 ":apiname($internalname)" and
":apiname($)" syntax would serve.
However, I also have a concern about copying the Perl 6 ":$name" syntax.
It's not a priori bad syntax in the Perl 5 context, but you're certainly
not getting the benefits that that syntax has in the Perl 6 context.
In Perl 6, in an ordinary expression context ":$name" is shorthand for
"name => $name", and the syntax for a named argument with API name
different from internal name likewise resembles a pair-construction
syntax. In the Perl 5 context, it's tempting to imitate pair syntax
afresh, getting "apiname => $internalname" and "apiname => $" for
the names-differing cases, but then we have no Perl 5 syntax for
the shorthand pair constructor. Perhaps the signature syntax for the
names-matching case should be "=> $name", although that can never become
the corresponding pair-constructing shorthand in expression context.
I'm also dubious about the way "\" and ":" interact. You show
"\:@coordinates". I think the naming indicator, whether ":" or "=>",
should go before the "\". "foo => \@foo" would accurately imitate a
potential call site, so seems less confusing than anything with the