Generic functor class in java

I would like to have a functor class as follows:

public class Functor<T, R> { 
    public R invoke(T a) { ... }
}

And one more class for two arguments:

public class Functor<T1, T2, R> { 
    public R invoke(T1 a, T2 b) { ... }
}

Etc.

In C #, I can write:

class Functor<T> { ... }
class Functor<T1, T2> { ... }

But in Java this will be an error:

The type Functor is already defined

What are the best methods for generic classes with multiple arguments in java?

+5
source share
2 answers

Since generic tools are implemented in Java, erasing type information (in class C<T>, Tdisappears in the compiled .class file), the compiler does not know which class you are talking at runtime.

If you define F<T1>and F<T1,T2>load both of them, some class Ccannot identify the one that he wanted to use.

, , , Java.:\

, , - F<T>, T - , Pair<T1, T2>, ThreeTuple<T1, T2, T3> .. Scala .

+5

? , , .

Scala Function; - jvm - , arity: Function0<Res>, Function1<T1, Res>, Function2<T1,T2, Res> .., Function22.

0

All Articles