S
- The input object type.T
- The output object type.A
- The input field type.B
- The output field type.public interface Prism<S,T,A,B> extends Optic<Cocartesian _,S::A,T::B>
A
from the input sum type S
and to build an object of sum type T
from a derived value B
.
The canonical example for using a prism is to extract and update a field of a tagged union
type, using
the C meaning of union
.
In order to be a lawful prism, the implementations of match(Object)
and build(Object)
must satisfy certain requirements. Assume that the object types S
and T
are implicitly convertible
between each other and that the field types A
and B
are similarly convertible. Then the following
rules must hold (==
here represents logical equality and not reference equality).
match(build(b)) == Right(b)
- Matching against a built value yields that value.
build?(match(s)) == s
- If a field is matched, building with it yields the original object.
Prisms that are not lawful are said to be either neutral or chaotic, depending on the degree to which the prism laws are broken.
Modifier and Type | Interface and Description |
---|---|
static class |
Prism.Instance<A2,B2>
The
Cocartesian type class instance for Prism . |
Modifier and Type | Method and Description |
---|---|
T |
build(B b)
Constructs a value of the output object type with the given output field value.
|
default <P extends K2> |
eval(Cocartesian P proof)
Evaluates this prism to produce a function that, when given a transformation between field types, produces a
transformation between object types.
|
(T|A) |
match(S s)
Attempts to extract the input field from the input object.
|
compose, composeUnchecked, upCast
(T|A) match(S s)
For example, say that the input object type is DataResult
and the output
object type is Optional
. Calling match
with the data result will return either the
value present in the result (if a DataResult
is a success), or an absent
Optional
(if the result is an error).
build(Object)
, satisfy the prism laws in order
for this prism to be a lawful prism.s
- A value of the input object type from which to attempt to extract the input field value.T build(B b)
For example, this may be used to construct an Optional
or DataResult
from a plain value.
match(Object)
, satisfy the prism laws in order
for this prism to be a lawful prism.b
- The value to construct the output object from.default <P extends K2> (P<A,B>) -> P<S,T> eval(Cocartesian P proof)
eval
in interface Optic<Cocartesian _,S::A,T::B>
P
- The type associated with the profunctor.proof
- The Cocartesian
type class instance for the transformation type.Prism.Instance