On Tuesday, 3 June 2014 at 00:57:53 UTC, Chris Cain wrote:
On Monday, 2 June 2014 at 23:01:56 UTC, deadalnix wrote:
On Monday, 2 June 2014 at 22:53:10 UTC, ponce wrote:
- no exceptions (!)
How do they do error handling ?
I haven't read too much into Swift but languages with ADTs,
pattern matching, and no exceptions can (and usually do) return
using an ADT looking something like (pseudocode):
adt Outcome(T) {
Error(ErrorType e);
Success(T result);
}
and then for a "try catch" type block, you do things with
switches
switch(http.getResponse(someServer)) {
case Success(r):
// ... use r
case Error(http.error.Http404(msg)):
// handle the 404 error
case Error(e):
return Error(e); // Let the caller handle it
}
Or some other similar construct. With macros you can easily
write something that tries to get the outcome's success and if
it can't, then it automagically returns the error for you,
throwing the error to the calling function like you expect from
exceptions. That said, if you're paying attention you'll see
the similarity to checked exceptions...
So basically, those two features give you what is effectively
exception handling, "nothrow", and so on. It's one of those
really powerful language features with a lot of leverage that
makes it easy to define constructs in the language instead of
the compiler.
Ocaml and Haskell also have ADT and exhaustive matching.
Yet Ocaml provides exceptions. Haskell provide the Exception
monad to avoid littering the code with error-checking.
Of course ADT are great and all but if error checking is verbose
it won't encourage people to check for errors.