Skip to content

Instantly share code, notes, and snippets.

@MarcelineVQ
Created September 3, 2020 19:06
Show Gist options
  • Save MarcelineVQ/488dc12bc4cb783eda77fecd425b3350 to your computer and use it in GitHub Desktop.
Save MarcelineVQ/488dc12bc4cb783eda77fecd425b3350 to your computer and use it in GitHub Desktop.
module Foo
import System.File
import Control.Monad.Managed
export
withFile' : HasIO io => String -> Mode -> (Either FileError File -> io a) -> io a
withFile' file mode act = do res <- openFile file mode
a <- act res
either (\_ => pure a)
(\f => pure a <* closeFile f) res
main : IO ()
main = do
putStrLn "No files are open"
runManaged $ do
f1 <- managed $ withFile' "/dev/urandom" Read
putStrLn "f1 is open"
f2 <- managed $ withFile' "foo.txt" Read
putStrLn "f2 is open"
dosomethingwith f1
dosomethingwith f2
putStrLn "No files are open"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment