X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=topics%2Fweek13_coroutines_exceptions_and_aborts.mdwn;h=a0b8d0d4d3ed3333d46a164b846408ebdfadd89c;hp=a9d8bc2a66aa15614367850ecdae09021f4cd691;hb=e335784b08032fe0078b9178b503a9dfb697f9e6;hpb=649716384d5e6e1a828d782369a80a80c4ae1a46 diff --git a/topics/week13_coroutines_exceptions_and_aborts.mdwn b/topics/week13_coroutines_exceptions_and_aborts.mdwn index a9d8bc2a..a0b8d0d4 100644 --- a/topics/week13_coroutines_exceptions_and_aborts.mdwn +++ b/topics/week13_coroutines_exceptions_and_aborts.mdwn @@ -212,17 +212,26 @@ Here we call `foo bar 0`, and `foo` in turn calls `bar 0`, and `bar` raises the OK, now this exception-handling apparatus does exemplify the second execution pattern we want to focus on. But it may bring it into clearer focus if we **simplify the pattern** even more. Imagine we could write code like this instead: - # let foo x = - try begin - (if x = 1 then 10 - else abort 20 - ) + 100 - end - ;; + let foo x = + try begin + (if x = 1 then 10 + else abort 20 + ) + 100 + end then if we called `foo 1`, we'd get the result `110`. If we called `foo 2`, on the other hand, we'd get `20` (note, not `120`). This exemplifies the same interesting "jump out of this part of the code" behavior that the `try ... raise ... with ...` code does, but without the details of matching which exception was raised, and handling the exception to produce a new result. -Many programming languages have this simplified exceution pattern, either instead of or alongside a `try ... with ...`-like pattern. In Lua and many other languages, `abort` is instead called `return`. In Lua, the preceding example would be written: +> If we had to write that using `try...with...`, it'd look something like this: + +> exception Abort of int;; (* declare a new type of exception that can carry an int parameter *) +> let foo x = +> try +> (if x = 1 then 10 +> else raise (Abort 20) +> ) + 100 +> with Abort n -> n + +Many programming languages have this simplified execution pattern, either instead of or alongside a `try ... with ...`-like pattern. In Lua and many other languages, `abort` is instead called `return`. In Lua, the preceding example would be written: > function foo(x) local value