Add withConcurrentVar, modify and modify_ #298
Conversation
A few useful methods for manipulating ConcurrentVars safely using bracketCase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add some tests? It's bracket
composition...
Could we add some docs to explain why these methods are favored like we discussed on Slack or in the STM PR?
) | ||
|
||
override suspend fun <B> modify(f: suspend (A) -> Tuple2<A, B>): B { | ||
// ugly. Is there a better way? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the fastest and simplest implementation afaik. Just a tad ugly yes 😅
Well it is just composition so I don't think being very exhaustive in the tests is necessary, although 1-2 basic tests can't hurt to at least show that it works. I'll add something to docs, although this is best explained in the context of how to use |
Great tests and docs, thanks @1Jajen1! |
Also fail faster by not waiting for unfulfilled promises
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks Jannis,
when (exit) { | ||
is ExitCase.Failure -> put(a) | ||
is ExitCase.Cancelled -> put(a) | ||
is ExitCase.Completed -> put(res!!) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
res?.let(::put)
maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should put(res!!)
ever fail for some reason I'd rather have it crash than not put back the value because that could lead to a very confusing deadlock.
A few useful methods for manipulating ConcurrentVars safely using bracketCase.