Skip to content
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

Merged
merged 1 commit into from
Feb 5, 2020
Merged

DIP: Named Arguments #168

merged 1 commit into from
Feb 5, 2020

Conversation

WalterBright
Copy link
Member

No description provided.

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.
Copy link
Contributor

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).

Copy link
Member

@CyberShadow CyberShadow Sep 15, 2019

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.)

Copy link
Contributor

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.

Copy link

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")).

Copy link
Member

@schveiguy schveiguy Jan 29, 2020

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 code

DIPs/7NNN-WGB.md Outdated

```
void snoopy(T t, int i, S s); // A
void snoopy(S s, int i = 0; T t); // B

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Member

@schveiguy schveiguy left a 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)
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is based

Copy link
Member Author

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.
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NamedArgument , ArgumentList

right?

Copy link
Member Author

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
Copy link
Member

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.

https://dlang.org/spec/grammar.html#ArgumentList

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@WalterBright
Copy link
Member Author

the type declaration that is used will dictate the parameter names recognized?

That's right. I amended the DIP to reflect this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants