dart-archive / ts2dart

ts2dart TypeScript to Dart transpiler
Apache License 2.0
181 stars 62 forks source link

Support generics on methods #282

Closed tbosch closed 8 years ago

tbosch commented 9 years ago

It would be nice to write something like this:

function add<NUM>(a:NUM):NUM {...}

and have the following in Dart:

/*<NUM>*/ add/*<NUM>*/(/*<NUM>*/ a){...}

This is needed in Angular e.g. for ViewContainerRef.createEmbeddedView: I would like to write this:

createEmbeddedView<C>(templateRef: TemplateRef<C>, context?: C,
                              index?: number): EmbeddedViewRef<C>

But right now ts2dart outputs this:

EmbeddedViewRef<C> createEmbeddedView/*< C >*/(TemplateRef<C> templateRef,
      [C context, num index]);

The tricky bit here is that this output of ts2dart would be correct if ViewContainerRef had a generic C declared on itself (e.g. class ViewContainerRef<C> { ...}). However, in this case, the generic originates just from the method itself.

yjbanov commented 8 years ago

Dart has temporary syntax for generic methods now that we should be able to emit TypeScript generic methods/functions/typedefs to. Let's do that.

mprobst commented 8 years ago

@yjbanov do you have a pointer to docs?

yjbanov commented 8 years ago

@jacob314 might have some

jacob314 commented 8 years ago

https://github.com/dart-lang/dev_compiler/blob/a1b57501c1528b7573f807e076e0b02aa83e5d35/GENERIC_METHODS.md

tbosch commented 8 years ago

@mprobst Hi, could you have a look at this?

mprobst commented 8 years ago

This is supported since quite a bit, it'll translate generic parameters on methods into Dart DDC's comment syntax.

tbosch commented 8 years ago

Just checked with the current version. If I write

createEmbeddedView<C>(templateRef: TempalteRef<C>, context: C, index: number):EmbeddedViewRef<C>

I am getting this:

EmbeddedViewRef<C> createEmbeddedView/*< C >*/(TemplateRef<C> templateRef,
      [C context, num index]);