Allow Functor.map() to return type-safe Functor subclasses

This commit is contained in:
Paul Campbell 2017-12-10 10:40:31 +00:00
parent 283e7a2f46
commit 41f9227afd
3 changed files with 6 additions and 5 deletions

View file

@ -34,18 +34,19 @@ import java.util.function.Function;
* </ul> * </ul>
* *
* @param <T> the type of the Functor * @param <T> the type of the Functor
* @param <F> the type of the mapped Functor
* *
* @author Tomasz Nurkiewicz (?@?.?) * @author Tomasz Nurkiewicz (?@?.?)
*/ */
public interface Functor<T> { public interface Functor<T, F extends Functor<?, ?>> {
/** /**
* Applies the function to the value within the Functor, returning the result within a Functor. * Applies the function to the value within the Functor, returning the result within a Functor.
* *
* @param f the function to apply * @param f the function to apply
* @param <R> the type of the result of the function * @param <R> the type of the content of the mapped functor
* *
* @return a Functor containing the result of the function {@code f} applied to the value * @return a Functor containing the result of the function {@code f} applied to the value
*/ */
<R> Functor<R> map(Function<T, R> f); <R> F map(Function<T, R> f);
} }

View file

@ -33,7 +33,7 @@ import java.util.function.Function;
* @author Paul Campbell (pcampbell@kemitix.net) * @author Paul Campbell (pcampbell@kemitix.net)
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
class Identity<T> implements Functor<T> { class Identity<T> implements Functor<T, Identity<?>> {
private final T value; private final T value;

View file

@ -37,7 +37,7 @@ import java.util.function.Supplier;
* @author Paul Campbell (pcampbell@kemitix.net) * @author Paul Campbell (pcampbell@kemitix.net)
*/ */
@RequiredArgsConstructor(access = AccessLevel.PROTECTED) @RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class Mon<T> implements Functor<T> { public class Mon<T> implements Functor<T, Mon<?>> {
/** /**
* The value. * The value.