4 ms·
I’ll also add that you can achieve some of these effects without linear types, but linear types are more flexible. For example, a classic way to work with a fil
by klodolph 6y ago
I’ll also add that you can achieve some of these effects without linear types, but linear types are more flexible. For example, a classic way to work with a file in Haskell is to use the function withFile:
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
You pass a function to withFile, and the file handle is valid for the scope of the function. However, this means you can’t do the following:
open file A
do stuff
open file B
do stuff
close file A
do stuff
close file B
Because the usage of files "A" and "B" don’t nest, you can’t achieve it with withFile. But you can get the same level of safety using linear types.
- tome 6y agowithFile also has the drawback that you can invalidly return the Handle from the handler.
- klodolph 6y agoThat’s true, but not much of a drawback because it’s not a likely error for humans to make and there aren’t really negative consequences.
- dllthomas 6y agoNegative consequences are whatever use-after-close errors you make trying to use the thing you returned. ... but I certainly agree that it's not an error that's made very often, and the flexibility is the bigger point.
- dllthomas 6y agoYeah, this is what I meant by "the bracket pattern" and a canonical case where it "doesn't fit" - I appreciate your spelling it out for those who are less familiar :)