I am trying to define Java classes that are similar to Haskell functors. Thus, a functor is defined as:
public interface EndoFunctor<X extends Type> extends Type { <Y extends Type> EndoFunctor<Y> fmap(Function<X,Y> map, EndoFunctor<X> fx); }
If I want to implement
functor, I have to write something like
public class Id<X extends Type> implements EndoFunctor<X> { protected X witness; Id(X witness) { this.witness = witness; } @Override public <Y extends Type> Id<Y> fmap(Function<X, Y> map, Id<X> fx) { return new Id<>(map.apply(fx.witness)); } }
The problem with this code is that the Id<X> does not match the EndoFunctor<X> . How can I define fmap in the EndoFunctor interface so that if some type K<T> implements EndoFunctor<T> , and the mapping function T->U , then K<U> returned as a value without any type casting ( what is it, since I know that my object is Id<T> , then the result of fmap "should be" a Id<U> , and therefore, I omit the result of type EndoFunctor<U> to this type)?
jackb source share