-
Notifications
You must be signed in to change notification settings - Fork 213
Support rest parameter syntax like Javascript. #893
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 can add a class Person {
final String name;
final int age;
Person({this.name, this.age});
@override
String toString() {
super.toString();
return 'name: ${name}, age: ${age}';
}
factory Person.fromJson(Map<String, dynamic> json) =>
Person(name: json['name'], age: json['age']);
}
main() {
var data = {'name': "hello", 'age': 18, 'sex': 'male'};
// var person1 = Person(data);
// var person2 = Person(...data);
// var person3 = Person({...data});
var person4 = Person.fromJson(data);
var person5 = Person.fromJson({...data});
var data2 = {'username': "hello", 'age': 18, 'sex': 'male'};
var person6 = Person.fromJson(data2);
print(person6); // name: null, age: 18
} |
What you are showing here is spread arguments. Simple spreading a map or list into a parameter list is not going to be statically typable. The reason you can just spread an iterable into a list literal is that as long as all the elements have the right type, it doesn't matter how many there are. That does not hold for arguments. If you could define a function as In any case, it's something we are aware of, but do not have a clean solution for right now. |
@irhn, How about named parameters and map, in the example above,
or
|
Dart is a statically checked language. Using a value with an unknown run-time structure, like a map, as parameters makes it impossible to statically check the validity of the arguments. Take the example: var data = {#name: "hello", #age: 18};
Person(...data) (Using symbols for referring to source names, same as In all actually useful cases, it's not possible to see statically which entries the map has, and which types the values are for each key. The type of the map, If Dart had typed structs/named tuples, it might be possible to do: // Static type is the named tuple type `(String name, int age)`
var data = (name: "name", age: 18);
var person = Person(...data); At this point, the static type of |
Kotlin has a keyword |
Varargs specifically (ie, simply collecting many objects into a Here's what @lrhn said in an earlier comment:
I suppose the main pushback would be "why not just pass in a regular List?", especially with the new list-comprehension tools and the spread operator. |
I posted a issue about spread syntax support in dart (#891), it was support in dart 2.3 acturally. Now it comes to it's partner
rest parameter
.I try to write the following code, but it has syntax error. Is there some simple and clean syntax to achieve the same goal like javascript?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters
The text was updated successfully, but these errors were encountered: