Created
September 3, 2020 19:06
-
-
Save MarcelineVQ/488dc12bc4cb783eda77fecd425b3350 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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