cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 158 forks source link

Subtle issue with generic interfaces/objects #56

Closed renaudpawlak closed 8 years ago

renaudpawlak commented 8 years ago

The following code does not pass TSC:

package source.generics;
import java.util.function.Consumer;
import jsweet.lang.Interface;

public class GenericObjectStructure {
    <T> void m(MyInterface<T> i, Consumer<T> c) {
    }
    public static void main(String[] args) {
        new GenericObjectStructure().m(new MyInterface<String>() {
            {
                f = "test";
            }
        }, param -> { param.indexOf("c"); });
    }
}

@Interface
abstract class MyInterface<T> {
    String f;
}

That's because the new MyInterface<String>() {{ f = "test"; }} type is erased to { f : "test" } when transpiled... As a consequence, the T parameter actual value cannot be inferred and param is not considered as a String anymore.

Objects and interfaces constructions must be correctly typed to avoid this problem.

renaudpawlak commented 8 years ago

All good!