-
Notifications
You must be signed in to change notification settings - Fork 207
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
Arguments shorthand #1123
Comments
So you mean like:
? You can do that...search for |
I know about that approach, and it's not what I wanted. I'm really talking about named parameters, just like we have in Javascript. |
This feature would use a variable in argument position, where it could/must be a named argument, to be treated as a named argument with the same name as the variable. That's not a feature you are going to create a new variable to use. It's going to be almost exclusively used to forward named parameters, the place where you currently write If we add such a feature, we might want to limit it to only forwarding parameters, so you can't do it with a local variable, or a global or instance variable for that matter. That would reduce the risk of accidentally triggering the feature. The biggest problem here is that it doesn't interact well if we ever allow the same function to have both optional positional and optional named parameters. fromStreetName(int id, String name) => foo(42, name); would you then expect the second argument to be a positional argument or a named argument (named This ambiguity means that there are some sharp edges where the feature would stop working, possibly unexpectedly. All in all, I think I'd prefer some other approach to making parameter forwarding easier, without linking variable names and named argument names. That feels a little too fragile. Not sure what those other approaches would be, though. |
Yes, like @lrhn says, I don't like the idea of an entirely bare identifier being a shorthand for passing a named argument with that name. It's fragile and potentially ambiguous. I could imagine some lightweight but still explicit notation like, I don't know: String hello({String name}) {
return "Hello $name";
}
var name = "Foo";
// Instead of hello(name: name), we could just:
hello(name:); But I don't know if that's too weird too carry its weight. |
@munificent I think it is explicit, but not too weird. So as an example:
|
That makes a kind of sense. The |
Wow guys, thank you so much for taking the time needed to participate on this! As I told before I wasn't really hopeful on this, but I'm glad you all brought some cool ideas! 😄 |
what about using the same token as in the declaration ? It's more streamlined this way
Could be ambiguous in some circumstances maybe ? The big advantage of using named params is to document the code. When the variable name is the same as the named param then it's duplicate documentation. I'm curious what's the proportion of named vs positional parameters in the packages on pub dev |
developer:log |
This is the main problem with this approach. At parse time this looks like a set literal. Using the context type it could work, but it just makes the grammar more ambiguous at parse time which is not desired. I think I still prefer the ; in the argument list since it is explicit and unambiguous. |
Oh yeah. Still, in the idea of keeping things streamlined I don't think using a new symbol entirely is a good idea. This at least looks a bit like current named params:
|
If we compare it with "if variables" (#1201), both needs some way to extract a name from an expression. Then |
Similar issue, #58. It seems like tuples and tuple destructuring into arguments of function would solve this use case and many others. |
That would be great. I don't want to go of topic but looking ahead, destructuring can be convenient too if you have a "parent" object, although you have to check the source to know which property is used so it might be a bit less readable:
|
Since Dart allows a similar shorthand for the upcoming pattern matching feature ( |
We'd have to define what the "implict name of an expression" is (the patterns feature only derives the implicit name of a pattern. Patters are part-declarations and part-expressions, and we only take the name from the declaration part. Maybe something like the following could define "expressions ending in an identifier" <namedExpression> ::=
<identifierExpression>
| <identifierExpression> '!'
| <identifierExpression> <postfixOperator>
| <assignableExpression> <assignmentOperator> <namedExpression>
<identifierExpression> ::=
<identifier>
| 'super' '.' <identifier>
| <primary> <selector>* ('.' | '?.') <identifier> and then I'm sure there are lots of gnarly corners to consider. |
I didn't find any issue talking about this (and to be honest I don't know if it's doable in Dart), but here we go:
The text was updated successfully, but these errors were encountered: