Skip to content

Instantly share code, notes, and snippets.

@parsonsmatt
Last active June 14, 2017 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsonsmatt/c5102a0db776d20a6ec925694a075ded to your computer and use it in GitHub Desktop.
Save parsonsmatt/c5102a0db776d20a6ec925694a075ded to your computer and use it in GitHub Desktop.
Implicit params instead of ReaderT
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE RankNTypes #-}
module Lib where
data Env = Env { envFoo :: Int, envBar :: String }
type App a = (?env :: Env) => IO a
someFunc :: IO ()
someFunc =
let ?env = Env 3 "hello"
in printFoo
-- λ ~/Projects/implicits/ stack build && stack exec implicits-exe
-- 3
-- 7
printFoo :: App ()
printFoo = do
foo <- asksIO envFoo
print foo
localIO (\e -> e { envFoo = 7 }) $ do
foo' <- asksIO envFoo
print foo'
askIO :: App Env
askIO = return ?env
asksIO :: (Env -> r) -> App r
asksIO f = fmap f askIO
localIO :: (Env -> Env) -> App a -> App a
localIO f action = do
env <- asksIO f
let ?env = env in action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment