Skip to content

Add stateTVar which I've found very helpful to cut boilerplate with - #14

Merged
hvr merged 1 commit into
haskell:masterfrom
dfinity-side-projects:master
Sep 18, 2018
Merged

hvr merged 1 commit into
haskell:masterfrom
dfinity-side-projects:master

Conversation

@infinity0

Copy link
Copy Markdown
Contributor

No description provided.

@simonmar

Copy link
Copy Markdown
Member

Maybe. The IORef version of this is called atomicModifyIORef, so it seems perhaps a little strange to call the TVar version stateTVar. But calling it atomicModifyTVar obviously would be even more strange.

I'm not feeling wildly excited but I'm also not feeling dead against :)

@infinity0

Copy link
Copy Markdown
Contributor Author

Yeah, naming is hard. I named it stateTVar by analogy with:

  • modifyTVar vs Control.Monad.Trans.State.{Strict,Lazy}.modify
  • modifyTVar' vs Control.Monad.Trans.State.{Strict,Lazy}.modify'
  • so stateTVar vs Control.Monad.Trans.State.{Strict,Lazy}.state seemed to continue the pattern for me.

It is definitely not the most "obvious" naming but I also couldn't yet think of better ones. In earlier versions of the code I used elsewhere, I called it runTVar.

@simonmar

Copy link
Copy Markdown
Member

Ok, that analogy seems reasonable. Let's go ahead and add this. The build is currently failing though, could you take a look?

@hvr

hvr commented Sep 18, 2018 •

Copy link
Copy Markdown
Member

@simonmar I think the build failure is accidental; and we can probably sneak this into 2.5.0 as long as @bgamari hasn't frozen the GHC 8.6.1 tree yet

TODOs:

  • Changelog entry
  • @since 2.5.0.0 Haddock annotation

@hvr
hvr merged commit 75f4aeb into haskell:master Sep 18, 2018
hvr added a commit that referenced this pull request Sep 18, 2018
@rehno-lindeque

rehno-lindeque commented Aug 27, 2019 •

Copy link
Copy Markdown

It's sort of unfortunate that atomicModifyIORef / modifyMVar ended up with the reversed order of the result pair compared to state. It's a little distracting when refactoring MVar to TVar.

stateTVar :: TVar s -> (s -> (a, s)) -> STM a
atomicModifyIORef :: IORef a -> (a -> (a, b)) -> IO b
modifyMVar :: MVar a -> (a -> IO (a, b)) -> IO b

@infinity0 infinity0 mentioned this pull request Jul 14, 2020
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

Successfully merging this pull request may close these issues.

4 participants