cats-effect
cats-effect copied to clipboard
Memoize on Resource is weird
Or rather, it's weird if you use the results. For example:
foo.memoize.flatMap(r => r.use(pure) *> r.use(pure))
The second use will end up accessing the resource after the finalizers have already run. It's not entirely clear what the ideal semantic here, but this seems… not great. One possible answer would be that the memoization needs to be re-run once the first use happens, but there's no way to do this generically, since memoize is implemented as a generic combinator on Concurrent.
This in turn raises an interesting question: is this an early indication that Concurrent[Resource] just doesn't make sense? Are there other such examples? Can we quantify the "weirdness" in terms of laws?
is this an early indication that
Concurrent[Resource]just doesn't make sense
I'm not sure. I feel like whether Concurrent[Resource] makes sense is being conflated with whether memoize can be implemented generically.
I don't remember seeing laws for memoize, but it seems like some could be written in terms of Ref. Namely, that the effect in question is run at most once, since that is what memoize is fundamentally about AFAICT.
So long as it is possible for Resource to implement said memoize lawfully, then I'd say Concurrent[Resource] makes fine sense.
Arman went and fixed this because that's what he does