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

Do you see value in a Ref-Monad like State? #32

Closed
rubenmoor opened this issue Nov 23, 2020 · 4 comments
Closed

Do you see value in a Ref-Monad like State? #32

rubenmoor opened this issue Nov 23, 2020 · 4 comments

Comments

@rubenmoor
Copy link

I am using purescript for a canvas-based web app and don't need any fancy UI library. I end up with a lean, global application state and deal with events quite bare-footed.

I find myself writing

app <- read refAppState
flip write refAppState $
    app { foo = newFoo
        , bar = app.bar + 1
        }

quite a lot.

If you don't object, I'll come up with a pull request soon to allow to write the above via a monad for effectfull, global state:

foo .= newFoo
bar += 1

I miss the lenses :)

Also, maybe you have a suggestion for the module name.

How about Effect.Ref.Monad? Alternatively Control.Monad.State.Global.

@hdgarrood
Copy link
Contributor

I think something along these lines is unlikely to be merged in this library; we prefer to keep core libraries as lean and unopinionated as is reasonably possible. Are you imagining something that is more or less equivalent to newtype RefM s m a = RefM (StateT (Ref s) m a)? Because I don't think that needs to exist; why not just use StateT directly?

@rubenmoor
Copy link
Author

rubenmoor commented Nov 24, 2020

Putting a reference into a local state (StateT (Ref s) m a or State (Ref s) a) doesn't make sense. What you mean is probably ReaderT (Ref s) m a or Reader (Ref s) a. That still would imply explicit use of write and read from Effect.Ref and does not allow anything close to the syntax above. Correct me if I'm wrong.

@hdgarrood
Copy link
Contributor

Yes, you're right, I did mean Reader rather than State. But in any case I don't think this is something we will want to include in this library.

@rubenmoor
Copy link
Author

rubenmoor commented Nov 24, 2020

My initial idea is complicated by the fact that a reference-based, global state monad, being effectful, cannot have an instance of the type-class MonadState, which is of course pure.

The only way out that I can see would be to re-implement an effectful variant of MonadState. This, in turn, implies a re-implementation of all related lenses. A huge copy-and-paste, might still be somewhat useful, but by no means elegant.

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

No branches or pull requests

2 participants