-
Notifications
You must be signed in to change notification settings - Fork 213
Named arguments #4185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm sure I have proposed something like this before, possibly as part of some other feature (I can't find an issue right now). It's a nice idea which allows users to refer to parameters by name, when they have a name, even if they are not named parameters. The risks are that the names are not really part of the function value, they're just metadata on the static function types.
|
In what I envisioned here
Is it that bad to have parameters name part of the public contract when compared with the potential benefits ?
The dynamic issue seems like a non issue to me. Dynamic usages are pretty seldom and if you really need to label, you can cast. |
Python here has positional only args: def foo(bar: str, /): ... If we decide to have nullable args / args with default as optional, we can reuse |
This is basically what my "Enhanced Normal Parameters" strawman is. I haven't written it up in public form yet, but maybe I should. It's how most other languages with named arguments work. There are some real trade-offs with it but I suspect that overall it's a better approach. Certainly it would make parameter list syntax cleaner and simpler. |
@munificent is it going to happen in the future altogether with: #2232? Lack of named arguments by default and treating optional only by passing Python approach here to treat specially only positional-only and keyword-only args is IMO much better approach. With these 2 approaches, |
Named argument
As opposed to named parameters, named arguments is about using the parameter name on the call site.
Named arguments enable you to specify an argument for a positional parameter by matching the argument with its name rather than with its position in the parameter list.
Example:
Rationale:
The decision to use named or positional arguments can be a decision the call site should make.
Private properties
This could lead to other beneficial features down the line like:
Declaring all fields in a constructor
Related: Object spread shorthand
Syntax sugar
...Object
is used in a constructor / function call to unpack labels:...Object
unpack the labels with the intersection between the parameters name and the object getters name.Copies with null values
The last declaration of the field wins, allowing copying with null values (when the fields are not private).
The text was updated successfully, but these errors were encountered: