dart-archive / ts2dart

ts2dart TypeScript to Dart transpiler
Apache License 2.0
180 stars 61 forks source link

Preserve generic methods for DDC #348

Closed mhevery closed 8 years ago

mhevery commented 8 years ago

Generic method:

  static T first<T>(List<T> list) => list.isEmpty ? null : list.first;

should translate to:

  static dynamic /*=T*/ first/*<T>*/(List/*<T>*/ list) => list.isEmpty ? null : list.first;

Notice the /*=T*/ when you are saying that dynamic is actually T vs /*<T>*/ when you are simply adding type information.

Used as:

  first<String>(...)

should translate to:

  first/*<String>*/(...)
mprobst commented 8 years ago

@jacob314 we'd like to do this, but are unclear on how to best represent the types for non-DDC users. E.g. for @mhevery's function declaration above, would we represent the return types best as dynamic /*=List<T>*/, or rather as List /*<T>*/?

dynamic would be easier for us, but it might make API consumption worse for non-DDC users (I guess?). I'm also not sure what the impact on dart2js would be from this.

Any advice?

vsmenon commented 8 years ago

@mprobst the function declaration above doesn't return a list, but one that did would look like:

List/*<T>*/ makeList/*<T>*/() => ...

There shouldn't be a case where you'd need to return a worse type for non-DDC users.

vsmenon commented 8 years ago

BTW:

List/*<T>*/

is shorthand for:

List<dynamic/*=T*/>

if the latter is easier?

vsmenon commented 8 years ago

thanks!

mhevery commented 8 years ago

Sorry still broken because the output can not have spaces. Should be first/*<String>*/(...) not first/* < String > */(...)

mprobst commented 8 years ago

@vsmenon the whitespace thing (within a comment!) seems really surprising, maybe this is a bug in DDC? Also: is there grammar or spec for this?

I guess we should have an integration test using DDC...

vsmenon commented 8 years ago

@mprobst (and @jmesserly who actually implemented this)

I believe it's just the spaces at the ends that are problematic. We only interpret comments of the form:

/*<TYPE_EXPRESSION_LIST>*/ and /*=TYPE_EXPRESSION*/

TYPE_EXPRESSION can have whitespace in it - first/*< String >*/ should be fine. It's just that we're filtering for /*< or /*= exactly to minimize accidentally picking up unintentional comments.

The actual proposed grammar is here: https://github.com/leafpetersen/dep-generic-methods/blob/master/proposal.md - but that has no comments. :-)

The temporary comment syntax is here: https://github.com/dart-lang/dev_compiler/blob/master/doc/GENERIC_METHODS.md