Skip to content

Memoize on Resource is weird #1620

Closed
Closed

Description

@djspiewak

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?

Activity

  1. armanbilge commented on Jul 7, 2022

    @armanbilge
    Member

    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.

  2. djspiewak commented on Oct 29, 2022

    @djspiewak
    MemberAuthor

    Arman went and fixed this because that's what he does

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions