Skip to content

Factory methods should have direct access to type parameters #22700

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
DartBot opened this issue Mar 6, 2015 · 7 comments
Closed

Factory methods should have direct access to type parameters #22700

DartBot opened this issue Mar 6, 2015 · 7 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

Comments

@DartBot
Copy link

DartBot commented Mar 6, 2015

This issue was originally filed by [email protected]


class WrapT<T> {
  Type get type => T;
}

class MyClass<T> {
  factory MyClass.works() {
    Type t = new WrapT<T>().type;
    print(t);
  }

  factory MyClass.fails() {
    print(T);
  }
}

main() {
  new MyClass<String>.works();
  new MyClass<String>.fails();
}

Right now, I get: cannot access type parameter 'T' from static function
As the example above shows T is available using wrapper code. So the error should not have to occur for factory methods.

Thanks,
Andreas

@DartBot
Copy link
Author

DartBot commented Mar 6, 2015

This comment was originally written by @mezoni


But the "factory" are syntactic sugar over the static methods.
How they can get access to type arguments if Dart language does not support generic methods?

Even if they in Dart available with an operator "new" this does not means that they are constructors.

This is also syntactic sugar over the static methods.

@DartBot
Copy link
Author

DartBot commented Mar 6, 2015

This comment was originally written by [email protected]


Thanks for the comment.

As you see above, factory methods have access to the generic type parameters. The first factory constructor actually works. It's just that wrapper code is needed to get around the unnecessary error.

@floitschG
Copy link
Contributor

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

@lrhn
Copy link
Member

lrhn commented Mar 6, 2015

I think this is a bug in the VM.

The spec says
... "Otherwise, the body of q is executed with respect to the bindings that
resulted from the evaluation of the argument list and the type parameters (if
any) of q bound to the actual type arguments V1, . . . , Vl resulting in an object
i. The result of the evaluation of e is i."

This says that the body is executed in a scope where the type parameter is in scope.
Similar code works in dart2js.


Removed Type-Enhancement, Area-Language labels.
Added Type-Defect, Area-VM labels.

@iposva-google
Copy link
Contributor

Gilad, please verify that the spec interpretation is correct and that the VM should allow direct access to T in the factory. Thanks!


cc @gbracha.
cc @crelier.

@crelier
Copy link
Contributor

crelier commented Mar 6, 2015

I also think it is a bug.


Set owner to @crelier.
Added Accepted label.

@crelier
Copy link
Contributor

crelier commented Mar 9, 2015

Fixed at r44339.

Corresponding issues have been opened for dart2js (22738) and analyzer (22739).


Added Fixed label.

@DartBot DartBot added Type-Defect area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. labels Mar 9, 2015
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
Projects
None yet
Development

No branches or pull requests

6 participants