Reader monad transform one env to another
See original GitHub issueDescription
Hi, I’ d like to chain two reader monads, where one env is subtype of the other, so inner function can access only type it requires and outer function can requires general env param.
Repro steps
type Env = {Now: DateTime; Conn: string}
type InnerEnv = {Now: DateTime}
let inner () =
monad {
let! (e:InnerEnv) = Reader.ask
return doWork e.Now
}
let outer () =
monad {
let! (e:Env) = Reader.ask
let! m = Reader (fun p -> {Now = p.Now})
let! r = inner()
let! m = Reader.local (fun (x:Env) -> {Now = x.Now})
return r
}
Expected behavior
Expected result is function outer with signature Reader<Env, Result>
Actual behavior
Doesn’t compile
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to get ReaderT to work with another monad ...
1 Answer 1 ... I've tested it, and it at least works. Whether or not this is best practices is unknown to me....
Read more >Control.Monad.Trans.Reader
Execute a computation in a modified environment (a specialization of withReaderT ). runReader ( withReader f m) = runReader m . f. The...
Read more >Note to self: reader monad transformer
A Reader is a data type that encapsulates an environment. The runReader function takes an environment (a Reader ) and runs it, producing...
Read more >Making Sense of Multiple Monads
In a monad transformer, the lift function allows you to run actions in the underlying monad. So using lift in the ReaderT Env...
Read more >Monad Transformers
Monad transformers allow us to combine the features of different monads in our program. This concept allows us to write extremely flexible programs....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found solution:
I see, it looks like I misunderstood your problem.
I was thinking in having a generic env with some constraints, and I came up once with a solution which requires SRTP accessor functions for each property, which is something like:
let inline getConn x = (^T : (member Conn : _) x)
but as I said, this is not what you need.