Skip to content

Type inference should exploit "is" and "is!" semantics #32236

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
chalin opened this issue Feb 19, 2018 · 4 comments
Closed

Type inference should exploit "is" and "is!" semantics #32236

chalin opened this issue Feb 19, 2018 · 4 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug

Comments

@chalin
Copy link
Contributor

chalin commented Feb 19, 2018

Consider this implementation of equals excerpt from the code shown in this section of the Library Tour:

  @override
  bool operator ==(dynamic other) {
    if (other is! Person) return false;
    Person person = other; // (*)
    return ...
  }

As of Dart 2-dev.26, the type of other at (*) is dynamic.
Couldn't it be inferred as Person?

Similar arguments can be given for an if condition that makes use of is.

cc @kwalrath @stereotype441 @leafpetersen @eernstg

@eernstg eernstg added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug labels Feb 20, 2018
@eernstg
Copy link
Member

eernstg commented Feb 20, 2018

The language specification gives some hints about the rationale for making exceptions about dynamic during type promotion (where we are considering v is T and S is the static type of v):

We do not want to refine the type of a variable of type \DYNAMIC{}, as this could lead to more warnings rather than fewer.
The opposite requirement, that $T \ne \DYNAMIC{}$ is a safeguard lest $S$ ever be $\bot$.

So the main point was, arguably, to let dynamic code be dynamic code.

We probably still want to maintain that no variable can be promoted to dynamic (which is unlikely anyway, because dynamic is now a top type), but support for promoting away from dynamic seems to be in line with the general development of the language from Dart 1 to Dart 2. I just marked this issue with area and type for a language enhancement request.

@leafpetersen
Copy link
Member

Promotion from dynamic is part of Dart 2, but promotion in the continuation of a conditional is not. I believe that the extended type promotion proposal that @bwilkerson wrote up handles this (and more), and that proposal had broad support on the language team. While technically breaking, I'm still hoping that we will be able to land this post Dart 2 either as a minimally breaking change, or by using some sort of opt-in mechanism.

@eernstg
Copy link
Member

eernstg commented Feb 21, 2018

Sorry, I overlooked that the example relies on return and is!. However, there is also #30906 which is about promotion and dynamic in Dart 2.

@lrhn
Copy link
Member

lrhn commented Jan 12, 2024

Done.

@lrhn lrhn closed this as completed Jan 12, 2024
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). type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants