Hello! I have a little doubt. When I try to modify...
# arrow
c
Hello! I have a little doubt. When I try to modify a field Option <String>, this is replaced by a String instead of a Option<String>
Copy code
ej : data class PersonalDataModel(
    val firstName: Option<String>
)

firstName.modify(state){ //here i have a String instead of Option<String> }
s
Are you using generated
optics
? The reason for that is that an
Optional
is generated instead of a
Lens
. https://arrow-kt.io/docs/0.10/optics/lens/ https://arrow-kt.io/docs/0.10/optics/optional/
Optional
is a combination of
copy
with
when
to
modify
or
getOption
the focus.
c
Copy code
@optics
data class PersonalDataModel(
    val firstName: InputModel,
    val secondName: InputModel,
    val firstLastName: InputModel,
    val secondLastName: InputModel,
    val birthDate: SpinnerModel,
    val nationality: SpinnerListModel,
    val birthPlace: Option<SpinnerModel>,
    val gender: SpinnerListModel
) {
    companion object
}

val initialState = PersonalDataModel(
           ......... ,
            birthPlace = None
        )

model.birthPlace.modify(state) { SpinnerModel("Birth Place", {}) } }
this i think that return None instead of the new object
s
That’s because you cannot apply
f
of
modify
over
None
c
What would the best approach to modify the initial None state?
s
In that case you probably want to use a
Lens<PersonalDataModel, Option<SpinnerModel>>
or
Lens<PersonalDataModel, SpinnerModel?>
. Which currently is not autogenerated. The reason you want to model it with a
Lens
instead of a
Optional
is because you then have the ability to control the
Option
rather than using the non-nullable
SpinnerModel
in
Optional
.
All the is used underneath is how you’d write it by hand. In the case of
Lens
you’re dealing directly with the properties of a
data class
and you can
set
or
get
them, abstracting over properties and
copy
. In the case of
Prism
you’re dealing with the properties of a subtype of a
sealed class
. This abstracts over
when
and
copy
, and it thus not have the same powers as
Lens
. Instead it can
getOption
the result from
when
or it attempt to
modify
if the correct subtype is found using
when
. An
Optional
is the composition of a
Prims
and
Lens
which thus it has the same limitations as both combined.
c
perfect I going to dig into all this stuff, I have used the optics annotation because is the least boilerplate implementation