-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
DIP: Named Arguments #168
DIP: Named Arguments #168
Conversation
| corresponding default value specified for that `Parameter`. If there is no default value, | ||
| it is an error. | ||
| 5. If there are more `NamedArgument`s than `Parameter`s, the remainder match the trailing | ||
| `...` of variadic parameter lists, and `Identifier`s are not allowed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't want to hold this up with my request, but I just wanna through out one idea that might be useful to me in some case: instead of banning identifiers on a variadic, actually use them to make an anonymous struct.
For example, given:
void foo(T...)(T args) {}
foo(a: 0, b: "zero");
Instead of issuing an error, that actually generates something like this:
struct _anonymous {
int a;
string b;
}
foo(_anonymous(0, "zero"));
and thus args[0] is actually an instance of this generated type, holding all the named parameters that matched the variadic.
A practical application of this would be library tuples with named members, or syntax sugar for creating dynamic (e.g. json) objects,or some cases of like database query DSLs.
I don't actually wanna sidetrack y'all too much, just throwing out this as something to consider as potential in changing this rule (or something).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So basically Python's kwargs?
I'm not against the idea in principle but I think it needs to be opt-in for the callee, otherwise, if a parameter is removed, the error messages are going to be very confusing. (Also that would put it out of scope for this DIP.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, basically. I am inclined to agree on being opt-in too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the feature. Another option would be named tuples foo((a: 0, b: "zero")).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @CyberShadow, the DIP as-is provides zero burden on the implementor, they don't have to change anything in any functions, it's just how you can call them is more pleasant.
The sort of change you are proposing would open up existing implementations to a lot of problems (and probably is a breaking change). I get the point of it, but probably not doable. Note that this would also cause problems with template instantiation, since separate calls for the same types would now be different instantiations, depending on the names used in the call.
However, template parameters might provide a mechanism for this. For example, you already have code like this:
template IKnowYourName(T...)
{
pragma(msg, __traits(identifier, T[0]));
}
int x;
alias n = IKnowYourName!x; // prints "x"
alias n2 = IKnowYourName!(blah: 0); // could also work, even with existing code1cc6068
to
fd22e63
Compare
DIPs/7NNN-WGB.md
Outdated
|
|
||
| ``` | ||
| void snoopy(T t, int i, S s); // A | ||
| void snoopy(S s, int i = 0; T t); // B |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is a typo? Otherwise I don't understand the semicolon or why i can have a default value with t requiring one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
1525865
to
8d99eab
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be something in there discussing lambdas and function types in general. I’m assuming that parameter names don’t affect the type but the type declaration that is used will dictate the parameter names recognized?
| 3. A step towards making it possible to replace the brace struct initialization syntax with | ||
| a function-call-like construct. I.e. the initialization of structs is unified with | ||
| struct literals and struct constructors. | ||
| 4. Allow replaceing [`std.typecons.Flag`](https://dlang.org/phobos/std_typecons.html#Flag) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacing
DIPs/7NNN-WGB.md
Outdated
|
|
||
| ## Description | ||
|
|
||
| This based on the recognition that D already has |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is based
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
DIPs/7NNN-WGB.md
Outdated
| For each `NamedArgument`: | ||
|
|
||
| 1. If an `Identifier` is present, it matches to the `Parameter` with the corresponding | ||
| `Identifier`. If it does match any named `Parameter`, then it is an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it does not match
DIPs/7NNN-WGB.md
Outdated
| ArgumentList: | ||
| NamedArgument | ||
| NamedArgument , | ||
| NamedArgument , NamedArgumentList |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NamedArgument , ArgumentList
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right
DIPs/7NNN-WGB.md
Outdated
|
|
||
| NamedArgument: | ||
| Identifier : Argument | ||
| Argument |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Existing grammar has this as AssignExpression, not Argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
That's right. I amended the DIP to reflect this. |
8d99eab
to
ccc2e10
Compare
No description provided.