Class Upcast<A extends B,​B>

  • Type Parameters:
    A - the covariant type
    B - the contravariant type
    All Implemented Interfaces:
    Fn1<A,​B>, Applicative<B,​Fn1<A,​?>>, Cartesian<A,​B,​Fn1<?,​?>>, Cocartesian<A,​B,​Fn1<?,​?>>, Contravariant<A,​Profunctor<?,​B,​Fn1<?,​?>>>, Functor<B,​Fn1<A,​?>>, Profunctor<A,​B,​Fn1<?,​?>>, Monad<B,​Fn1<A,​?>>, MonadReader<A,​B,​Fn1<A,​?>>, MonadRec<B,​Fn1<A,​?>>, MonadWriter<A,​B,​Fn1<A,​?>>

    public final class Upcast<A extends B,​B>
    extends java.lang.Object
    implements Fn1<A,​B>
    Upcast a value of type B to a value of type A that B extends. This is principally useful when dealing with parametric types that are invariant in their parameters and a cast is necessary for compatibility purposes.

    Example:

     
     Iterable<String> have = new ArrayList<>();
     Iterable<CharSequence> want = map(upcast(), have); // necessary due to invariance in parameter
     
     

    Note that this is universally safe.

    • Field Detail

      • INSTANCE

        private static final Upcast<?,​?> INSTANCE
    • Constructor Detail

      • Upcast

        private Upcast()
    • Method Detail

      • checkedApply

        public B checkedApply​(A a)
        Description copied from interface: Fn1
        Invoke this function with the given argument, potentially throwing any Throwable.
        Specified by:
        checkedApply in interface Fn1<A extends B,​B>
        Parameters:
        a - the argument
        Returns:
        the result of the function application
      • upcast

        public static <A extends B,​B> Upcast<A,​B> upcast()
      • upcast

        public static <A extends B,​B> B upcast​(A a)