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
Hello. I've not found a way to update Left(prev_value) to Right(new_value) in case when I need the previous value to create the new one. In Option we have the method take that replaces an old value with None, and returns the old one to us. It would be nice to have something like this:
pubfnmorph(&mutself,f:F,g:G) -> Either<L,R>whereF:FnOnce(L) -> R,G:FnOnce(R) -> L
Now to do this, I have to use Option wrapping Either to be able to have the third None state.
But actually I don't need it, and it makes me write more code for checking the option's state later. I just want to have a member with two correct states: L and R.
Thanks
The text was updated successfully, but these errors were encountered:
Either can't really make this any easier itself, as it would face all the same limitations, with no knowledge of what's safe or desirable for any given L or R. Using Option::take is an example of that "sentinel" pattern in the blog, but maybe in your particular case, L or R might have a cheap empty state that you could use as a temporary instead.
It looks like now it can't be implemented in a reasonable way. Actually it possible to implement a struct wrapper that hides None sentinel from the user:
structEitherEx<L,R>{value:Option<Either<L,R>>}impl<L,R>EitherEx<L,R>{pubfnget(&self) -> &Either<L,R>{
value.as_ref().unwrap()// or even unchecked, because we should guarantee correctness}pubfnmorph(...){...}// now it is possible to implement this like in the example above}
Also I think it would be possible to implement this if we could add a private enum element that the user can't get access to. I found some pages about this: one, two. Perhaps one day we'll see it in rust :)
Hello. I've not found a way to update
Left(prev_value)
toRight(new_value)
in case when I need the previous value to create the new one. InOption
we have the methodtake
that replaces an old value withNone
, and returns the old one to us. It would be nice to have something like this:Now to do this, I have to use
Option
wrappingEither
to be able to have the thirdNone
state.But actually I don't need it, and it makes me write more code for checking the option's state later. I just want to have a member with two correct states:
L
andR
.Thanks
The text was updated successfully, but these errors were encountered: