You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/exceptions-best-practices.md
+25Lines changed: 25 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -206,6 +206,31 @@ at this point in the spectrum. I've proposed to the libraries mailing list to
206
206
add a new method to the `Exception` typeclass used for user-friendly display of
207
207
exceptions, which will make this less of a sore point.
208
208
209
+
## Comment on Reddit
210
+
211
+
Austin beat me to the punch on most of my response. To make one of my points more concrete, consider if I have a thread with the code:
212
+
213
+
withFile "input.txt" ReadMode $ \inH ->
214
+
withFile "output.txt" WriteMode $ \outH ->
215
+
hGetContents inH >>= hPut outH
216
+
217
+
How would your "finaliser action" allow us to guarantee that `inH` and `outH` are always closed? I see no way of achieving that.
218
+
219
+
I *don't* believe there is a better solution to sync exceptions, actually. That's because most of the time I see people complaining about `IO` throwing exceptions, what they *really* mean is "this specific exception just bit me, why isn't this exception explicit in the type signature?" To clarify my point further:
220
+
221
+
* There are virtually 0 `IO` actions that can't fail for some reason.
222
+
* If every `IO` action returned a `IO (Either UniqueExceptionType a)`, the programming model would become *incredibly* tedious.
223
+
* If instead every `IO` action returned `IO (Either SomeException a)`, we'd at least not have to deal with wrangling different exception types, and could use `ErrorT` to make our code simpler, but...
224
+
* Then we've just reinvented exactly what `IO` does today, only less efficiently!
225
+
226
+
My belief is that people are simply ignoring the reality of the situation: the contract for `IO` implicitly includes "this action may also fail." And I mean in every single case. Built in, runtime exceptions hide that in the type, but you need to be aware of it. Runtime exceptions *also* happen to be far more efficient than using `ErrorT` everywhere.
227
+
228
+
And as much as some people complain that exceptions are difficult to handle correctly, I highly doubt `ErrorT` or anything else would be easier to work with, we'd just be trading in a well-developed, mostly-understood system for a system we think we understand.
229
+
230
+
Come up with a concrete "no more exceptions" proposal, and I'll discuss it more concretely. But "there are better solutions, we all know it" isn't a discussion point.
0 commit comments