Closed ParetoOptimalDev closed 1 year ago
Continuing to try and figure this out for myself...
I can't quite understand it that way because the scope of Managed doesnt stay alive through multiple functions but using the with pattern it would.
For instance:
main :: IO ()
main =
withFile "inFile.txt" ReadMode $ \inHandle ->
useHandle1 inHandle
useHandle2 inHandle
useHandle1 handle = do
putStrLn "pretend this is writing to handle"
useHandle2 handle = do
putStrLn "pretend this is writing to handle"
I think then I'll want to somehow follow this documentation:
If you need to acquire a resource for a long-lived loop, you can instead acquire the resource first and run the loop in IO, using either of the following two equivalent idioms:
with resource (\r -> forever (useThe r))
do r <- resource
liftIO (forever (useThe r))
I tried code following that last piece of documentation and it didn't seem to help here:
-- doesn't work
try1 = (Turtle.sh $ do
tmpDir <- Turtle.mktempdir "/tmp" "turtle"
liftIO $ useTmpDir1 tmpDir
liftIO $ useTmpDir2 tmpDir)
-- doesn't work
try2 =
let tmpDir = Turtle.mktempdir "/tmp" "turtle"
in Turtle.with tmpDir $ \d -> do
useTmpDir1 d
useTmpDir2 d
Another thing to try I could think of:
try3 = Turtle.runManaged $ do
tmpDir <- Turtle.mktempdir "/tmp" "turtle"
useTmpDir1 tmpDir
useTmpDir2 tmpDir
Given that the liftio example from try1
exists in the dhall codebase I'm assuming that this not working has more to do with me using ghci an dthings will work fine if I just run the binary.
It does, closing this.
Given:
I get:
I've re-read https://hackage.haskell.org/package/managed-1.0.9/docs/Control-Monad-Managed.html a few times and tried envisioning what the above code would look like in terms of something like:
But it's just not clicking for whatever reason.
Can anyone tell me how I should ensure the two helper functions stay within the scope of that Managed resource and if it's not too much to ask, what it'd look like in nested form?