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

Create an issue #57024

Closed
tolotrasamuel opened this issue Nov 5, 2024 · 2 comments
Closed

Create an issue #57024

tolotrasamuel opened this issue Nov 5, 2024 · 2 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-as-intended Closed as the reported issue is expected behavior triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality

Comments

@tolotrasamuel
Copy link

tolotrasamuel commented Nov 5, 2024

Dart infers type incorrectly with inheritance.

Dart type inference is not working properly with inheritance and field overriding.

If we don't explicitly write final Dog pet = Dog(); (repetitive) the analyzer thinks that pet is Animal. (which is not wrong but problematic, it should be Dog)

class Animal {}

class Dog extends Animal {
  void bark() {
    print("Daawg!");
  }
}

abstract class Owner {
  Animal get pet;
}

class DogOwner extends Owner {
  @override
  final pet = Dog();
}

void main() {
  final dog = Dog();
  dog.bark(); // pass

  final owner = DogOwner();
  owner.pet.bark();  // error. Why ??
}

However, DogOwner.pet is clearly of type Dog (at least for the human reader)

Is this a bug in Dart or intended ? I am curious to know why would this be intended.

Dart version 3.4.3

@dart-github-bot
Copy link
Collaborator

Summary: The issue reports that Dart's type inference incorrectly infers the type of Foo.baz as Baz instead of Boz when Boz extends Baz and Foo overrides Bar.baz with a Boz instance. The user is unsure if this is intended behavior or a bug.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality labels Nov 5, 2024
@eernstg
Copy link
Member

eernstg commented Nov 5, 2024

You need to declare that the type of the getter in DogOwner is Dog:

class DogOwner extends Owner {
  @override
  final Dog pet = Dog();
}

When you don't declare the return type of a getter (in this case: the implicitly induced getter of the instance variable DogOwner.pet), the step which is called override inference prevails over the kind of inference that provides the type of a variable based on its initializing expression. So DogOwner.pet has the type Animal rather than Dog because it overrides Owner.pet which has type Animal.

This is a language design choice. It would certainly have been possible to give the initializing expression the higher priority.

However, with this design choice we give priority to the public interface of the class by using the inherited return type, (which is definitely part of the public interface of the superclass) and not the type of the initializing expression (which is an implementation detail). I think this is a useful design choice.

In any case, it's working as intended. So I'll close the issue.

@eernstg eernstg added the closed-as-intended Closed as the reported issue is expected behavior label Nov 5, 2024
@eernstg eernstg closed this as completed Nov 5, 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). closed-as-intended Closed as the reported issue is expected behavior triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

3 participants