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
From:
Juerd Waalboer
Date:
January 24, 2022 11:31
Subject:
Re: Named argument syntax (was Re: PSC #049 2022-01-07)
Message ID:
20220124113042.GK7670@k
Darren Duncan skribis 2022-01-23 23:03 (-0800):
>The main problem I see with "=" is that seems to conflict 
>with the fact that Perl allows assignments to be embedded 
>in the middle of expressions, so how would a user know 
>that "f($x = $y)" is not an assignment but rather is 
>setting a named argument.

In the same way any other syntax would be learned, I think. However, I think that f($x = $y) must remain compatible with existing code, and keep passing the value assigned to $x as a positional argument. The syntax for named argument passing that I described never uses a sigil on the LHS.

As for terminology, isn't setting a named argument also a kind of assignment, though? After all, it's basically a reflection of the subroutine signature in which "=" is also used.

>Actually this raises the very important matter, which is 
>the same issue no matter what syntax we use to indicate 
>named, in that will Perl support the argument name coming 
>from an arbitrary expression or variable, or whether only 
>a hard-coded parameter-name literal is allowed:
>
>  my $argname = "x";
>  f($argname = 42);
>  f(x = 42);
>
>So are the second 2 lines considered equivalent or is the 
>middle line a parse/compile error?

Neither. In my opinion, the second middle line should remain compatible with current practice in any case.

My suggestion that you're replying to actually covers this issue. I've suggested f("$argname" = 42) for the case where the name of the argument is not hardcoded.

>>For the common case of f(arg = $arg, foo = $foo, bar = 
>>$bar) where the argument and variable have the same 
>>name, I suggest adding prefix "=":  f(=$arg, =$foo, 
>>=$bar). And, for symmetry, a prefix "=>" for the fat 
>>comma equivalent, because that would also be useful when 
>>populating hashes and using legacy pseudo-named 
>>arguments.
>Yes, and I would argue in favor of the "=$foo" shorthand 
>as well, whether spelled that or ":$foo" etc, which Raku 
>introduced me to.

While I like having a shorthand available, I personally struggle with Raku's use of the colon here, because it's different from the infix operator for named arguments:

     # Raku
     f :$foo;
     f :foo($foo);
     f foo => $foo;

These lines are equivalent, even though the colon pair syntax and the fat arrow pair syntax look very different. When Perl gets proper named arguments, I hope it's possible to have the things that do the same look more alike.


-- 
Met vriendelijke groet, // Kind regards, // Korajn salutojn,

Juerd Waalboer  <juerd@tnx.nl>
TNX

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