dart-lang / dart-lang/language

Allow optional defaults to be properties of mandatory positional parameters

Open
#951 3 comments 1 reaction 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Describe the new syntax or language feature you'd like to see incorporated into
Dart. Ideally you can relate this to a `request` issue filed separately.

let's say I have some class:
```Dart
class Product{
final String id;
final String title;
final String description;
final double price;
final String imageUrl;
bool isFavourite;

Product({
this.id,
this.title,
this.description,
this.price,
this.imageUrl,
this.isFavourite=false,
});

_editedProduct = Product(id: null, title: null, description: null, price: null, imageUrl: null)
```

And in my buisness logic I want to update each element of the class at different times and in an unknown order (e.g with user input). As the properties are final, each time I have to create a new product with the other, unedited, fields saved fed back into the class

Lets say in an anonymous function associated with the user inputting the id.
```Dart
onSubmit: (value) {
_editedProduct = Product(
id: value,
title: _editedProduct.title,
description: _editedProduct.description,
price: _editedProduct.description,
imageUrl: _editedProduct.imageUrl);
}
```
If I wanted to do the same with the other fields, I could write
```Dart
onSubmit: (value) {
_editedProduct = Product(
id: _editedProduct.id,
title: value,
description: _editedProduct.description,
price: _editedProduct.description,
imageUrl: _editedProduct.imageUrl);
}

...

onSubmit: (value) {
_editedProduct = Product(
id: _editedProduct.id,
title: _editedProduct.title,
description: value,
price: _editedProduct.description,
imageUrl: _editedProduct.imageUrl);
}
```
And so on...

This leads to a lot of duplicated code.

It would be great if I could define a function like this:
```Dart
Product editProduct(Product productToEdit,
{String id = productToEdit.id,
String title = productToEdit.title,
String description = productToEdit.description,
double price = productToEdit.price,
String imageUrl = productToEdit.imageUrl}) {
return Product(
id: id,
title: title,
description: description,
price: price,
imageUrl: imageUrl);
}
```

Then to update any one paremeter I could simply run:
```Dart
_editedProduct = editProduct(_editedProduct, price = 4.55)
```
I see no reason why this shouldn't be possible. If a positional argument is mandatory (it is by definition) and the class is given (it is) then there is no reason why it would be error prone to access the properties of the positional argument in the definitions of the optional argument(s).

Current errors:

> The default value of an optional parameter must be constant.dart(non_constant_default_value)
> Undefined name 'productToEdit'.
> Try correcting the name to one that is defined, or defining the name.

Proposed Fix:
1. To allow the use of non-constant positional arguments
2. (If required) to bring the name definition of madatory parameters forward in time to always predate the definition of optional parameters so it is in memory and ready to use.

---------------------------------------------------------------------------------------------------------------------------------

A workaround is to do something like:

```Dart
Product _editProduct(Product productToEdit,
{String id,
String title,
String description,
double price,
String imageUrl}) {
Map temp = {
'id': productToEdit.id,
'title': productToEdit.title,
'description': productToEdit.description,
'price': productToEdit.price,
'imageUrl': productToEdit.imageUrl,
};
print(temp);
({
'id': id,
'title': title,
'description': description,
'price': price,
'imageUrl': imageUrl
}).forEach((k, v) {
if (v != null) {
temp[k] = v;
print(v);
}
});
print(temp);

return Product(
id: temp['id'],
title: temp['title'],
description: temp['description'],
price: temp['price'],
imageUrl: temp['imageUrl']);
}
```
Which could then be called as:
```Dart
onSaved: (value) {
_editedProduct = _editProduct(_editedProduct, title: value);
},
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.