You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What would you like to see?
Currently, we can compose Optionals, but we cannot do the same for Lenses with a nullable target type. Composing such lenses is very useful when there is a nested nullable path, but you still want to treat the final property as a nullable instance of that property, instead of an Optional.
So, consider:
@optics
data classOuter(valinner:Inner? = null) { companionobject }
@optics
data classInner(valfoo:String? = null) { companionobject }
// I would like to be able to doOuter.nullableInner.nullableFoo.get(instance) // get null when either inner or foo are null// Now I can only doOuter.inner.foo.getOrNull(instance)
// orOuter.inner.nullableFoo.getOrNull(instance)
// but there are no extension properties forOuter.nullableInner.???
While, of course, the Optional route works fine in most cases, I am using some custom logic that needs to deal with lenses for any kind of property, and I do not want to treat all properties as nullable (by accepting Optionals everywhere) and I prefer to keep the "nullability" of a property just a detail of the usage, where the nullability just comes out of the type parameters (you pass it a Lens with nullable target, you get an instance of a nullable type out as well)
My current workaround is using a custom conversion, like the following:
fun <S, A> Optional<S, A>.toNullableLens(): Lens<S, A?> =Lens(
{ s -> getOrNull(s) },
{ s, a ->if(a ==null) s else set(s, a) }
)
The text was updated successfully, but these errors were encountered:
Note that in Arrow 2.0 fields of nullable types will generate Lens instead of Optional, as explained in this section of the docs. Unfortunately this change cannot be made before, because it break compatibility with our current users.
In any case, I'm wondering if what you're proposing here is a new type of composition which would with nullable types. In general, if we have Lens<A, B?> and Lens<B, C>, we can build a new Lens<A, C?> with the method you describe. Note that this is not exactly the same as the optional composition.
I had missed that this will change with v2.0. I am curious to see how things work there! But indeed, you can see this as some "new" composition type, however, that might become "messy" with things like reverseGet I am afraid.
What version are you currently using?
1.2.0
What would you like to see?
Currently, we can compose
Optional
s, but we cannot do the same forLens
es with a nullable target type. Composing such lenses is very useful when there is a nested nullable path, but you still want to treat the final property as a nullable instance of that property, instead of anOptional
.So, consider:
While, of course, the
Optional
route works fine in most cases, I am using some custom logic that needs to deal with lenses for any kind of property, and I do not want to treat all properties as nullable (by acceptingOptional
s everywhere) and I prefer to keep the "nullability" of a property just a detail of the usage, where the nullability just comes out of the type parameters (you pass it aLens
with nullable target, you get an instance of a nullable type out as well)My current workaround is using a custom conversion, like the following:
The text was updated successfully, but these errors were encountered: