Skip to content

Class inherit from generic parameter type? #3833

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
hqakva opened this issue May 24, 2024 · 3 comments
Closed

Class inherit from generic parameter type? #3833

hqakva opened this issue May 24, 2024 · 3 comments
Labels
request Requests to resolve a particular developer problem

Comments

@hqakva
Copy link

hqakva commented May 24, 2024

Does Dart allow to create a class like this?

class GenericBased<T extends Object> extends T {
    final bool myProperties;

    const GenericBased({required this.myProperties})
}
@hqakva hqakva added the request Requests to resolve a particular developer problem label May 24, 2024
@jakemac53
Copy link
Contributor

No, Dart does not allow this.

@lrhn
Copy link
Member

lrhn commented May 24, 2024

The closest thing is a mixin, but mixins cannot have the constructor you need.

mixin MyProperty {
  bool property = false;
}

class FooWithProperty extends Foo with MyProperty {
  FooWithProperty(bool property) {
    this.property = property;
  }
} 

Without a constructor, it cannot add a final variable that is initialized to different values in different places.

@mateusfccp
Copy link
Contributor

mateusfccp commented May 25, 2024

Seems like this is a duplicate of #3552, although this one seems more like a question than a request.

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
Projects
None yet
Development

No branches or pull requests

5 participants