Closed as not planned
Description
The current use of null
for optional parameters in methods like copyWith
can lead to ambiguity. Specifically, null
is used to signify "leave unchanged", making it difficult to explicitly set a field to null
. This inconvenience complicates the handling of optional fields.
Introduce an undefined
, nil
, or similar type or keyword to represent omitted values, distinct from null
.
For example:
class User {
final String? name;
final int? age;
User({this.name, this.age});
User copyWith({
String? name,
int? age,
}) {
return User(
name: name == undefined ? this.name : name,
age: age == undefined ? this.age : age,
);
}
}
void main() {
User user = User(name: 'John', age: 30);
User newUser1 = user.copyWith(name: 'Jane');
print(newUser1);
// Output: User(name: Jane, age: 30)
User newUser2 = user.copyWith(name: null);
print(newUser2);
// Output: User(name: null, age: 30)
}