Skip to content

Introduce undefined type or keyword to differentiate intentional absence of value and uninitialized state #4239

Closed as not planned
@nathnaeld

Description

@nathnaeld

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) 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions