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:
Dan Book
Date:
January 24, 2022 05:03
Subject:
Re: Named argument syntax (was Re: PSC #049 2022-01-07)
Message ID:
CABMkAVVJvEbRpe+CGkmKLqaV7EKBRUWhg2yzx==r+=fB0gzrow@mail.gmail.com
On Sun, Jan 23, 2022 at 3:56 PM Juerd Waalboer <juerd@tnx.nl> wrote:
> TL;DR: the case for naming arguments with "=".
>
> Darren Duncan skribis 2022-01-21 21:38 (-0800):
> >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 "=>".
>
> I like the suggestion of leaving "=>" alone (in Raku I find it really
> annoying that it's used both for creating Pairs and for named arguments,
> which makes passing a Pair rather cumbersome), but,
>
> >I propose following an example which is very common in
> >other languages and data formats, and is also better
> >Huffman coded, which is to use the colon ":" to indicate
> >named parameters or arguments.
>
> As someone who uses lots of ternaries, I'm not so sure about this specific
> character. ":" is also involved in decorating sort in another proposal, and
> is already used for attributes and labels. Larry Wall's "first rule of
> language redesign", which asserts that "everyone wants the colon", is very
> true. Because so many people want the colon, it might make sense to pick
> something else whenever feasible.
>
> >This shouldn't conflict with any other use of the colon that I'm aware of.
>
> Maybe not technically, but visually it's really the same colon as every
> other colon.
>
> As a counter proposal I suggest "=", because it's nicely consistent with
> aesthetics of signatures. If named arguments have no sigil and I don't
> think anyone is currently suggesting giving them any, the distinction
> between assignment to a variable and to a named parameter should be easy
> enough. If computed argument names are required, those could be simply
> quoted, because assignment to a literal string is currently not a thing you
> can do.
>
> e.g.
>
> # not: f(name => "value");
> # not: f(name: "value");
>
> # but:
>
> f(name = "value");
> f("positional value", name = "value");
> f name = "value";
> f "name" = "value";
>
> Run-time computed named argument:
> f("$name" = "value");
>
> # unchanged:
> f $variable = "positional";
>
> Other languages that use "=" for named arguments include Python, R,
> Fortran, Julia, Kotlin, Maple, and OpenSCAD.
>
> 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.
>
This syntax is already valid and could be used to assign to an lvalue sub
(though doesn't seem likely to be common).
-Dan
Thread Previous
|
Thread Next