someReader :: Has Int t => Reader t Int
someReader = do
x <- ask
return $ get x + 1
Then i can run it with any tuple with an Int field like:
runReader someReader (0 :: Int, "adad”) -- 1
This typeclass almost solved all problem of my network application: sometime’s i want ensure a logger, a sql backend and a http client pool in my monad’s environment, but i don’t want to fix my environment into a record.
We can add a set :: a -> t -> t, or use lens to define Has, so that we can have extensible states.
We can also use Tagged to achieve something like:
(Has (Tagged “SqlBackEndOne” SqlBackEnd) t, Has (Tagged “SqlBackEndTwo" SqlBackEnd) t) => Reader t ()
It there a library doing this, maybe in lens? or there’re some drawbacks i didn’t notice? All ideas are welcomed!
Cheers~
Winter