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

Constructing instances of type arguments #3633

Closed
DartBot opened this issue Jun 14, 2012 · 7 comments

Comments

@DartBot
Copy link

commented Jun 14, 2012

This issue was originally filed by @olostan


Currently when compiling such code:

class A {
}

class B<T> {
  T GetBySmth() {
    return new T();
  }
}

void main() {
  var b = new B<A>();
  var x = b.GetBySmth();
  assert(x is B);
}

does not compile with message:

Error: line 6 pos 16: type parameter 'T' cannot be instantiated
    return new T();
               ^

So proposal is to add specifications to inform VM/compiler that generic type does have constructor, so it could be invoked to initialize instance.

Syntax could be (as proposed here https://groups.google.com/a/dartlang.org/d/msg/misc/2vgshmBCZx8/FdI6iKQvEgYJ):

class B<T is new()> {}
class B<T is new(int arg1)> {}

or

class B<T has new(int arg1)> {}

also with named constructors:

class B<T has new.namedConstructor(int arg1)> {}

This will allow to build much more clean generic behaviors (e.g. generic caches, repositories, etc)

@DartBot

This comment has been minimized.

Copy link
Author

commented Jun 14, 2012

This comment was originally written by @olostan


"assert(x is B);" should be "assert(x is A);", of course

@anders-sandholm

This comment has been minimized.

Copy link
Contributor

commented Jun 14, 2012

Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@gbracha

This comment has been minimized.

Copy link
Contributor

commented Jun 26, 2012

This is non-trivial issue. We've discussed it before, and will again.


Set owner to @gbracha.
Added this to the Later milestone.
Added Accepted label.

@kasperl

This comment has been minimized.

Copy link
Contributor

commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl

This comment has been minimized.

Copy link
Contributor

commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@gbracha

This comment has been minimized.

Copy link
Contributor

commented Feb 25, 2015

Issue #22424 has been merged into this issue.

@zoechi

This comment has been minimized.

Copy link
Contributor

commented Sep 14, 2015

I guess https://github.com/gbracha/metaclasses/blob/master/proposal.md is supposed to lead to a solution (as reference for people finding this issue first).

@kevmoo kevmoo added P2 type-enhancement and removed accepted labels Feb 29, 2016

@munificent munificent changed the title Initializing from type parameter (Generics) Constructing instances of type arguments Dec 19, 2016

@matanlurey matanlurey closed this Jun 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.