A
- the first possible typeB
- the second possible typeCP2
- the recursive type of this coproduct (used for embedding)@FunctionalInterface public interface CoProduct2<A,B,CP2 extends CoProduct2<A,B,?>>
Learn more about Coproducts.
Modifier and Type | Method and Description |
---|---|
default <C> CoProduct3<A,B,C,? extends CoProduct3<A,B,C,?>> |
diverge()
Diverge this coproduct by introducing another possible type that it could represent.
|
default <R> R |
embed(Fn1<? super CP2,? extends R> aFn,
Fn1<? super CP2,? extends R> bFn)
Embed this coproduct inside another value; that is, given morphisms from this coproduct to
R , apply
the appropriate morphism to this coproduct as a whole. |
default CoProduct2<B,A,? extends CoProduct2<B,A,?>> |
invert()
Swap the type parameters.
|
<R> R |
match(Fn1<? super A,? extends R> aFn,
Fn1<? super B,? extends R> bFn)
Type-safe convergence requiring a match against all potential types.
|
default Product2<Maybe<A>,Maybe<B>> |
project()
Project this coproduct onto a product, such that the index in the product that corresponds to this coproduct's
value is present, while the other indices are absent.
|
default Maybe<A> |
projectA()
Convenience method for projecting this coproduct onto a product and then extracting the first slot value.
|
default Maybe<B> |
projectB()
Convenience method for projecting this coproduct onto a product and then extracting the second slot value.
|
<R> R match(Fn1<? super A,? extends R> aFn, Fn1<? super B,? extends R> bFn)
R
- result typeaFn
- morphism A -> R
bFn
- morphism B -> R
default <C> CoProduct3<A,B,C,? extends CoProduct3<A,B,C,?>> diverge()
CoProduct3<String, Integer,
Boolean>
is expected, a CoProduct2<String, Integer>
should suffice.
Generally, we use inheritance to make this a non-issue; however, with coproducts of differing magnitudes, we
cannot guarantee variance compatibility in one direction conveniently at construction time, and in the other
direction, at all. A CoProduct2
could not be a CoProduct3
without specifying all type parameters
that are possible for a CoProduct3
- more specifically, the third possible type - which is not
necessarily known at construction time, or even useful if never used in the context of a CoProduct3
. The
inverse inheritance relationship - CoProduct3
< CoProduct2
- is inherently unsound, as a
CoProduct3
cannot correctly implement match(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends R>, com.jnape.palatable.lambda.functions.Fn1<? super B, ? extends R>)
, given that the third type C
is always possible.
For this reason, there is a diverge
method supported between all CoProduct
types of
single magnitude difference.
C
- the additional possible type of this coproductCoProduct3
<A, B, C>default Product2<Maybe<A>,Maybe<B>> project()
default Maybe<A> projectA()
default Maybe<B> projectB()
default CoProduct2<B,A,? extends CoProduct2<B,A,?>> invert()
default <R> R embed(Fn1<? super CP2,? extends R> aFn, Fn1<? super CP2,? extends R> bFn)
R
, apply
the appropriate morphism to this coproduct as a whole. Like match(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends R>, com.jnape.palatable.lambda.functions.Fn1<? super B, ? extends R>)
, but without unwrapping the
value.R
- result typeaFn
- morphism A v B -> R
, applied in the A
casebFn
- morphism A v B -> R
, applied in the B
case