3 ms·
I know this is facile, and it's speaking as someone with a lot more enthusiasm about than expertise with Haskell, but whenever I write some toy Haskell code and
by ben0x539 4y ago
I know this is facile, and it's speaking as someone with a lot more enthusiasm about than expertise with Haskell, but whenever I write some toy Haskell code and have to think about "remember to close this file handle before the end of this block" or "make sure not to use this file handle outside this block (because we just used a combinator to close it)", I'm baffled that Rust has mostly figured out that whole class of problem before Haskell
- tome 4y ago> I'm baffled that Rust has mostly figured out that whole class of problem before Haskell Have you heard of `withFile`? https://www.stackage.org/haddock/lts-20.16/base-4.16.4.0/System-IO.html#v:withFile https://www.stackage.org/haddock/lts-20.16/base-4.16.4.0/Sys...
- Yoric 4y agoNote that a `Handle` can escape from `withFile` (unless perhaps this library has been updated to use linear types, I haven't followed Haskell in a while). That being said, even without linear types, it's much harder to accidentally leak a scoped value in Haskell than in most other programming languages, as the leak shows up in the type signature. /pedantic
- tome 4y ago> Note that a `Handle` can escape from `withFile` Yes, this is true. If someone really wanted they could write a version that didn't using an ST-like type variable trick. I guess it hasn't been considered sufficiently necessary.
- ben0x539 4y agoYeah, that's what my second gripe was meant to complain about. Really, withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r is just a more general way of writing withFile :: FilePath -> IOMode -> (Handle -> IO Handle) -> IO Handle ;) I've also heard about the ResourceT monad transformer but as of right now I don't think I could explain what it does or judge how well it does it, maybe that's also a sufficient solution.
- tome 4y agoAh sure, fine, but see https://news.ycombinator.com/item?id=35345367 https://news.ycombinator.com/item?id=35345367