S - The input object type.T - The output object type.A - The input field type.B - The output field type.public interface Lens<S,T,A,B> extends Optic<Cartesian _,S::A,T::B>
A from the input object type S and
to combine the input S and the transformed field type B into the output object type T.
The canonical example for using a lens is to extract and update a field of a struct type, using the
C meaning of struct.
In order to be a lawful lens, the implementations of view(Object) and update(Object, 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).
update(b2, update(b1, s)) == update(b2, s) - Updating twice is equivalent to updating once.
view(update(b, s)) == b - Viewing after an update yields the value used to update.
update(view(s), s) == s - Updating with a viewed value yields the original object.
Lenses that are not lawful are said to be either neutral or chaotic, depending on the degree to which the lens laws are broken.
| Modifier and Type | Interface and Description |
|---|---|
static class |
Lens.Instance<A2,B2>
|
| Modifier and Type | Method and Description |
|---|---|
default <P extends K2> |
eval(Cartesian P proofBox)
Evaluates this lens to produce a function that, when given a transformation between field types, produces
a transformation between object types.
|
T |
update(B b,
S s)
Combines a value of the output field type with a value of the input object type to produce a combined value
of the output object type.
|
A |
view(S s)
Extracts a value of the input field type from the input object type.
|
compose, composeUnchecked, upCastA view(S s)
This method is analogous to a "getter" in traditional object-oriented programming.
update(Object, Object), satisfy the lens laws
in order for this lens to be a lawful lens.s - A value of the input object type.T update(B b, S s)
This method is analogous to a "setter" in traditional object-oriented programming. Note that the given
field b does not necessarily have to correspond with the straightforward transformation from the
input object to the output object.
view(Object), satisfy the lens laws in order
for this lens to be a lawful lens.b - A value of the output field type with which to update the output object.s - A value of the input object type to be converted to a value of the output object type.default <P extends K2> (P<A,B>) -> P<S,T> eval(Cartesian P proofBox)
eval in interface Optic<Cartesian _,S::A,T::B>P - The type of transformation.proofBox - The Cartesian type class instance for the transformation type.Lens.Instance