Skip to content
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

if null expression, analyzer need enhanced type verification #49080

Open
CyJaySong opened this issue May 23, 2022 · 1 comment
Open

if null expression, analyzer need enhanced type verification #49080

CyJaySong opened this issue May 23, 2022 · 1 comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@CyJaySong
Copy link

CyJaySong commented May 23, 2022

class Agent {
  late int id;
  late String name;

  Agent.fromJson(Map<String, dynamic> json) {
    id = json['id'] ?? 0;
    name = json['name'] ?? '';
  }
}

When the previous data type is dynamic, analyzer can't check that the type of 0 and 'name' match properly;
json['name '] or 0 is finally assigned to name. Wouldn't it be better if json['name'] and 0 were verified with name respectively? json['name'] is dynamic, so it will not be verified, but 0 is not, then, the verification of name and 0 can not be ignored.

@CyJaySong CyJaySong changed the title if null expression, need enhanced type verification if null expression, analyzer need enhanced type verification May 23, 2022
@eernstg
Copy link
Member

eernstg commented May 23, 2022

To find the type of an expression of the form e1 ?? e2, the type of e1 is computed (let's call that T1) then the part of that type which allows null is eliminated, if possible (let's call that result T1n for "T1 non-nullable"), and then the upper bound with the type of e2 is taken (UP(T1n, T2)).

The type dynamic doesn't allow for removing null (Object contains the correct set of objects for this purpose, but it doesn't have the same behavior with this set of objects as dynamic does), so when T1 is dynamic then T1n is dynamic as well.

So id = json['id'] ?? e2 allows e2 to be an expression whose static type is almost any type (it can be String or int or MyClass<Whatever>, but it can't be void).

This means that we won't get the same typing precision as we would have had with id = e2. So it would be great if that situation could be improved. We have had proposals for improvements to this situation (and a very similar one involving conditional expressions, ?:), e.g., dart-lang/language#1618 (comment).

@lrhn lrhn added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label May 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

3 participants