Hello! I would like to have a Reader which gives me multiple functions. For example, if I have two possible databases, it does not make sense to read from database A and delete from the other.
Here is a made up example with 1 function
initA :: String -> Effect Unit
initA param = log $ "init A " <> param
main :: Effect Unit
main = runReader go initA
go :: ReaderT (String -> Effect Unit) Identity (Effect Unit)
go = do
result <- ask
pure $ result "works"
“init A works”
2 functions:
getFromA :: String -> Effect String
getFromA param = pure $ "from A " <> param
initB :: String -> Effect Unit
initB param = log $ "init B " <> param
getFromB :: String -> Effect String
getFromB param = pure $ "from B " <> param
type System
= { init :: String -> Effect Unit, get :: String -> Effect String }
systemA :: System
systemA = { init: initA, get: getFromA }
systemB :: System
systemB = { init: initB, get: getFromB }
main' :: Effect Unit
main' = (runReader go' systemA) >>= log
go' :: ReaderT (System) Identity (Effect String)
go' = do
result <- ask -- <-Error
result.init "works"
pure $ result.get "works"
Could not match type Effect with type ReaderT { get :: String -> Effect String, init :: String -> Effect Unit} Identity
Can somebody explain why the first one works, the last one doesn’t?