Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ref.lens constructor #827

Merged
merged 2 commits into from
May 2, 2020
Merged

Add Ref.lens constructor #827

merged 2 commits into from
May 2, 2020

Conversation

jwojnowski
Copy link

I find Ref.lens useful in tests, where a single Ref aggregating state of multiple mocks can be transformed into a few "focused" Refs, ready for use in the mocks.

I'm not sure, though, if it should be a part of cats-effect or rather some small satellite library, so I'll leave it up to you to decide 😉

@kubukoz
Copy link
Member

kubukoz commented Apr 6, 2020

Some discussion on Gitter about the semantics (changes to A that don't involve changes in B): https://gitter.im/typelevel/cats-effect-dev?at=5e8b0e65d021c45cc7c995a6

@rkrzewski
Copy link
Contributor

I was looking at https://github.com/oleg-py/meow-mtl recently and I believe it covers your use case. It will both derive a MonadState[F, A] for a Ref[F, A] and also derive MonadState[F, Part] for a MonadState[F, Whole] whenever a Lens[Whole, Part] exists.

Of course meow-mtl involves a fair bit of magic based on even more magic in shapeless so I absolutely see merit in your lightweight solution!

@kubukoz
Copy link
Member

kubukoz commented Apr 10, 2020

@rkrzewski we're using meow-mtl as well, the problem is that MonadState doesn't offer a method like modify from Ref :( and I haven't been able to get it done in typelevel/cats-mtl#120 so far.

@kubukoz
Copy link
Member

kubukoz commented Apr 29, 2020

@djspiewak @SystemFw thoughts? I think this belongs here, but I remember you had different opinions on it

@SystemFw
Copy link
Contributor

On balance, I like it and I'd like to see it included

Copy link
Member

@rossabaker rossabaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it.

@@ -226,6 +226,23 @@ object Ref {
*/
def unsafe[F[_], A](a: A)(implicit F: Sync[F]): Ref[F, A] = new SyncRef[F, A](new AtomicReference[A](a))

/**
* Creates an instance focused on a component of another Ref's value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should briefly mention that updates in the parent that don't change the child won't disable the setter in the child's access.

@djspiewak djspiewak merged commit 912bb20 into typelevel:master May 2, 2020
@kubukoz
Copy link
Member

kubukoz commented Jun 17, 2020

Hey @djspiewak, any chance we can release this soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants