Skip to content

New of T #4333

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
loic-sharma opened this issue Apr 22, 2025 · 0 comments
Open

New of T #4333

loic-sharma opened this issue Apr 22, 2025 · 0 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@loic-sharma
Copy link

loic-sharma commented Apr 22, 2025

Introduction

It would be nice if Dart could instantiate a generic type. For example:

T myFactory<T>() {
  return T();
}

This would likely need some sort of generic constraint to ensure T can be instantiated.

Use case

Today, a Flutter StatefulWidget must implement the createState method:

class Foo extends StatefulWidget {
  const Foo();

  @override
  State<Foo> createState() => _FooState();
}

This language feature would be a step towards removing the createState method: StatefulWidget would accept a generic parameter and use that to provide a default createState implementation:

- class Foo extends StatefulWidget {
+ class Foo extends StatefulWidget<_FooState> {
    const Foo();
-
-   @override
-   State<Foo> createState() => _FooState();
  }

Previous discussions

The following issues appear relevant:

  1. could support T : new() like C#? #1787
  2. More capable Type objects #4200

Feel free to close this if there is a better tracking issue already :)

@loic-sharma loic-sharma added the feature Proposed language feature that solves one or more problems label Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

1 participant