Is there any way to express f-bound types in java where at the call site, a generic response is returned?
interface Functor<T extends Functor<T>>
public <B> T<B> map(Function<A, B> fn); // won't compile because types don't match
I can use f-bound types if the type never changes, but in the case of map, I need a new type. Is there a way to express this in java?
What I am really looking for is any way that I can get something like higher kinds even though I know javac doesn't support higher kinded types.
Lets say we have a List<A>
and want this interface to return a List<B>
. But don't want this interface to know anything about List
.
Reading the Wikipedia definition of functor, it sounds like you want to define a generic type which is able to map from one category (Java type) to another. In your example above, to map from a
List<A>
toList<B>
where the typesA
andB
are generic.If this is your aim, then consider the following interface for the definition of a
Functor
type:This declares that the
Functor
type deals in two generic parameter types,CategoryA
andCategoryB
and no constraints are put on these parameter types. It also declares that a methodmap
must be implemented which maps from an object of typeCategoryA
to an object of typeCategoryB
.Suppose, based on your example, you now want to create a concrete instance of a
Functor
which maps fromList<Integer>
toList<String>
. You might create the following class:You could then invoke this concrete
Functor
implementation like so:Now any method which is expecting a parameter of type
Functor<List<Integer>, List<String>>
can accept an instance of typeIntegerListToStringList
.