Closed josh-degraw closed 3 years ago
@josh-degraw the current behavior is correct according to the Optional
laws
I otherwise need to so something else like using a Lens<S, Option> and mapping the value of that
That's right, you need a Lens
.
Okay, fair enough if I misunderstood the intended behavior, my bad. It may be worth making some clarifications in the docs on the difference between when you would want to use an Optional<S,A>
vs using a Lens<S, Option<A>>
, since as a newcomer to using lenses it was not clear when you would use one over the other (even looking at the laws, and already having a decent amount of experience with fp, just not with lenses).
I spent a few hours trying to figure out why my setter was not being respected and some sort of documentation to clarify these points would be incredibly useful. Maybe even just a disclaimer on the differences between Option
and Optional
would be helpful, if not a few examples or utilities demonstrating how to work with the different use-cases.
Are there any existing utilities to assist in working with Lens<S, Option<A>>
? There are many things that are available via Optional<S,A>
such as optional.prop(string)
et. al. that AFAIK will require a lot more manual tweaking when using Lens<S, Option<A>>
. Any help or pointing in the direction of existing documentation would be greatly appreciated.
Hi @josh-degraw I am struggling with the same usecase. How have you solved it on your end, in particular when it comes to nested optional properties such as in
interface Inner {
someprop?: string
}
interface Outer {
inner?: Inner
}
how would a les for someprop.inner
be constructed and used?
π Bug report
Current Behavior
Using
set
to assign anoptional
, doesn't seem to work like I would expect. I apologize if I'm just not understanding properly. Basically if I have anOptional<S,A>
that currently doesn't have a value, and I callset(someValue)
, it results in a noop, and the value is not set.It currently only seems to set the value when the
getOption(S)
returnsSome
, which is quite frustrating as I otherwise need to so something else like using aLens<S, Option<A>>
and mapping the value of that.Expected behavior
I would expect that after calling
set
, regardless of whether or not a value existed previously, the value passed toset
should be present on the resulting object.Reproducible example
Your environment
Which versions of monocle-ts are affected by this issue? Did this work in previous versions of monocle-ts?