Skip to content

if-null operator disables type checking #3843

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

Open
abdallah-elsehaily opened this issue May 28, 2024 · 1 comment
Open

if-null operator disables type checking #3843

abdallah-elsehaily opened this issue May 28, 2024 · 1 comment
Labels
request Requests to resolve a particular developer problem type-inference Type inference, issues or improvements

Comments

@abdallah-elsehaily
Copy link

abdallah-elsehaily commented May 28, 2024

When using if-null operator ?? in property initialization of a class properties it disables the type checking as I will be able to assign bool to int as follows

class CustomerDetails {
  int numOfDays;

  CustomerDetails.fromJson(Map<String, dynamic> json)
      : numOfDays = false ?? json['numOfDays'] ?? false;
}
@lrhn
Copy link
Member

lrhn commented May 28, 2024

This is a known issue. The upper bound of bool and dynamic is dynamic, which is assignable to int.

The easiest fix to the language would be "Always coerce before LUB". which would coerce the dynamic from json['numOfDays'] to int before doing the least-upper-bound with bool.

Until then write it as false?? (json['numOfDays'] as int?) ?? false and get all the errors the code deserves. (Or at least some of them.)

@lrhn lrhn transferred this issue from dart-lang/sdk May 28, 2024
@lrhn lrhn added type-inference Type inference, issues or improvements request Requests to resolve a particular developer problem labels May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem type-inference Type inference, issues or improvements
Projects
None yet
Development

No branches or pull requests

2 participants