Skip to content

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

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

Closed
nathnaeld opened this issue Jan 28, 2025 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@nathnaeld
Copy link

nathnaeld commented Jan 28, 2025

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) 
}
@nathnaeld nathnaeld added the feature Proposed language feature that solves one or more problems label Jan 28, 2025
@nathnaeld nathnaeld changed the title Introduce undefined type to differentiate intentional absence of value and uninitialized state Introduce undefined type or keyword to differentiate intentional absence of value and uninitialized state Jan 28, 2025
@mmcdon20
Copy link

Similar idea to #3680.

@lrhn
Copy link
Member

lrhn commented Jan 28, 2025

Not a new idea. It has been discussed before, and having a first-class undefined type just pushes the problem one turtle further down, and having a non-first-class undefined type makes it effectively not really a type anyway. Having an undefined value that doesn't have a type is going to break a lot of useful invariants (like "values have types").

Not going there. A specific operation to check the initialization state of a late variable, like #3680, won't introduce any new types or values, so it's much more palatable. Other similar ways to work around the issue without reifying the lack of a value as, well, a value, could also work.
But undefined is right out.

@mateusfccp
Copy link
Contributor

Duplicate of #877.

@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

4 participants