Last active
June 14, 2017 15:30
-
-
Save parsonsmatt/c5102a0db776d20a6ec925694a075ded to your computer and use it in GitHub Desktop.
Implicit params instead of ReaderT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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