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

Allow type variables, not just type literals, as type parameters #11033

Closed
justinfagnani opened this issue Jun 2, 2013 · 3 comments
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug

Comments

@justinfagnani
Copy link
Contributor

For type checking on certain factory functions, it'd be very nice to allow specifying type parameters from variables, not just literals.

Example:

class A<T> {
  String toString() => "A<$T>";
}

A makeA(Type t) => new A<t>();

main() {
  var a = makeA(String);
  print("a = $a");
}

@lrhn
Copy link
Member

lrhn commented Jun 3, 2013

That would be a different way to get type-parametereized methods.
An alternative syntax could be:
...
  A makeA<S>() => new A<S>();
  main() {
   var a = makeA<String>();
   print("a = $a");
  }
...

This has the advantage that the compiler knows that S is a type, where your suggested syntax can't rely on the "Type" type-annotation in unchecked mode, and should have to dynamically check that "t" is actually a type. It could be any variable (and then, why not any expression, except that it would make parsing very very hard).

So, is this a duplicate of issue #254, or is the different syntax important?

@gbracha
Copy link
Contributor

gbracha commented Jun 3, 2013

I see this as a duplicate of issue #254. The specific suggestion has pros and cons. In an ideal world, I would implement generics using first class type objects passed as ordinary parameters and dispense with the entire edifice of special syntax etc. However, for Dart I think this is rather unlikely. So the more probable course of action would involve traditional generic methods as suggested in issue #254. Even that is debatable.


Added Duplicate label.
Marked as being merged into #254.

@justinfagnani
Copy link
Contributor Author

I know that this overlaps with issue #254 somewhat, but sometimes you do have a Type in a variable, and even with generic methods you'd need a way to place the variable in a type parameter position. As long as we have semi-first-class type values this will come up in some cases.

In one of my cases I'm using Types as keys in a map. If I want to construct a new type parameterized object from a key, generic methods don't help.

I would propose un-duping this issue.

@justinfagnani justinfagnani added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report labels Jun 3, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants