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:
String hello({String name}) {
return "Hello $name";
}
var name = "Foo";
// Instead of hello(name: name), we could just:
hello(name);
So you mean like:
String hello(String name) {
return "Hello $name";
}
?
You can do that...search for positional arguments and named parameters in the language tour 馃挴
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 bar({name}) => foo(name: name) and become annoyed by the repetition.
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.
Say we allow you to write foo(int id, [String? address], {String? name}), then someone writing:
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 name)?
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
In Julia's recent release, they have an explicit notation where a semicolon in the argument list denotes that all trailing arguments after the semicolon are named arguments with the name being the same as the name of the variable passed in.
I think it is explicit, but not too weird.
So as an example:
String hello({String name, String lastName, int age}) { return "Hello $name $otherName $age"; } var name = "Foo"; var age = 10; hello(lastName: 'other' ; name, age);
That makes a kind of sense. The ; is a separator, which otherwise cannot occur inside parentheses. Here it separates parameters being passed from (likely) parameters being forwarded. It's not a bad syntax, or semantics. It might look a little weird doing foo(;bar), but I think it's something you can easily get used to.
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! :smile:
Most helpful comment
That makes a kind of sense. The
;is a separator, which otherwise cannot occur inside parentheses. Here it separates parameters being passed from (likely) parameters being forwarded. It's not a bad syntax, or semantics. It might look a little weird doingfoo(;bar), but I think it's something you can easily get used to.