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.
The following code does not pass TSC:
That's because the
new MyInterface<String>() {{ f = "test"; }}
type is erased to{ f : "test" }
when transpiled... As a consequence, theT
parameter actual value cannot be inferred andparam
is not considered as aString
anymore.Objects and interfaces constructions must be correctly typed to avoid this problem.