Right now the signature of Ref.update is:
def update(f: A => A): UIO[A]
In some cases it can be useful to get the updated value back but in many cases it can be suboptimal because we instead need to return a UIO[Unit], requiring calling unit after each update operation. For example, from TestRandom:
/**
* Clears the buffer of booleans.
*/
val clearBooleans: UIO[Unit] =
bufferState.update(_.copy(booleans = List.empty)).unit
From our own code bases, it appears that most usages discard the value, but there are some cases where it is used. So it seems like it would be nice to have combinators for both varieties. I had initially thought of leaving update the same and adding update_ for the variant that returns UIO[Unit] even though I don't love the _ suffixes. @ghostdogpr suggested changing update to return UIO[Unit] and adding something like updateAndGet for the less commonly used value returning variant, which also seems quite nice.
What do other people think?
I always can鈥檛 remember if update returns the value before the update or the value after. So I鈥檓 in favor of more explicit naming and returning Unit for that one.
I like updateAndGet. Mirrors AtomicReference nicely.
I think this type is necessary to obtain an atomic set-and-get.
Well it could be done with modify but I agree it is nice not to have to return the tuple so would be good to keep a variant with the current type signature.
Didn鈥檛 read the whole thing, totally in favour of a more explicit name 馃憤
Most helpful comment
I always can鈥檛 remember if update returns the value before the update or the value after. So I鈥檓 in favor of more explicit naming and returning Unit for that one.
I like updateAndGet. Mirrors AtomicReference nicely.