Skip to content

Allowing the instantiation of a generic type by calling a no-arg constructor #54106

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
lukehutch opened this issue Nov 21, 2023 · 2 comments
Closed

Comments

@lukehutch
Copy link

I want to be able to instantiate generic types, whenever those types have a no-arg constructor:

class Foo<T> {
  T createT() => T();
}

This seems to be the best that can be done right now:

class Foo<T> {
  final T Function() newT;
  Foo(T Function() newT);

  T createT() => newT();
}

void x() {
  final foo = Foo<Bar>(Bar.new);
  final bar = foo.createT();
}

(I'm sure this has been raised before, although I can't find an issue on this...)

@mraleph
Copy link
Member

mraleph commented Nov 21, 2023

This is duplicate of dart-lang/language#1425 (other issues with discussions #3633, #7021, #24343, #30074).

For a long time we use to reject this feature, though I think there is some shifts in the language team which might indicate that we need to reconsider.

/cc @leafpetersen This is related to our discussions regarding behaviours tied to reified type parameters. Maybe we should reopen one of the issues to track the possibility that we might eventually implement this?

@mraleph mraleph closed this as completed Nov 21, 2023
@leafpetersen
Copy link
Member

Re-opened the referenced issue to serve as a the canonical tracking bug for the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants