InvalidTypesException: Type of TypeVariable 'K' in 'class X' could not be determined

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

InvalidTypesException: Type of TypeVariable 'K' in 'class X' could not be determined

Miguel Coimbra
Hello,

I have some code which compiles correctly (Flink 1.4) under Java 8.
It uses generic types.
While it compiles correctly, the execution fails with the error:

org.apache.flink.api.common.functions.InvalidTypesException: Type of TypeVariable 'K' in 'class X' could not be determined.

This is my main:

public static void main(final String[] args) {
X<Long> x = new X();
}

This is my class X:

public class X<K> {
    public X() {
TypeInformation<K> keySelector = TypeInformation.of(new TypeHint<K>(){});
}
}


Perhaps I'm lacking knowledge on the way Java's generics work, but why can't Flink determine the TypeVariable of 'K'?
As I am instantiating X parameterized as a Long, that information should eventually reach Flink and the constructor of X would be equivalent to this:

public X() {
TypeInformation<Long> keySelector = TypeInformation.of(new TypeHint<Long>(){});
}
During execution, however, this error pops up.
What am I missing here, and what is the best way to achieve this generic behavior in a Flink-idiomatic way?

Thank you very much for your time.
Reply | Threaded
Open this post in threaded view
|

Re: InvalidTypesException: Type of TypeVariable 'K' in 'class X' could not be determined

Timo Walther
Hi Miguel,

the issue that you are observing is due to Java's type erasure.

"new MyClass<String>()" is always erasured to "new MyClass()" by the Java compiler so it is impossible for Flink to extract something.

For classes in declarations like

class MyClass<String> extends ... {
   ...
}

the compiler adds the actual generic and Flink can extract it. So for classes the generics remain but generics passed to objects are erasured.

Regards,
Timo

Am 16.08.18 um 22:28 schrieb Miguel Coimbra:
Hello,

I have some code which compiles correctly (Flink 1.4) under Java 8.
It uses generic types.
While it compiles correctly, the execution fails with the error:

org.apache.flink.api.common.functions.InvalidTypesException: Type of TypeVariable 'K' in 'class X' could not be determined.

This is my main:

public static void main(final String[] args) {
    X<Long> x = new X();
}

This is my class X:

public class X<K> {
    public X() {
        TypeInformation<K> keySelector = TypeInformation.of(new TypeHint<K>(){});    
    }
}

Perhaps I'm lacking knowledge on the way Java's generics work, but why can't Flink determine the TypeVariable of 'K'?
As I am instantiating X parameterized as a Long, that information should eventually reach Flink and the constructor of X would be equivalent to this:

public X() {
    TypeInformation<Long> keySelector = TypeInformation.of(new TypeHint<Long>(){});    
}
During execution, however, this error pops up.
What am I missing here, and what is the best way to achieve this generic behavior in a Flink-idiomatic way?

Thank you very much for your time.