|
290 | 290 | (array/concat accum body)
|
291 | 291 | (tuple/slice accum 0))
|
292 | 292 |
|
293 |
| -(defmacro try |
294 |
| - ``Try something and catch errors. `body` is any expression, |
295 |
| - and `catch` should be a form, the first element of which is a tuple. This tuple |
296 |
| - should contain a binding for errors and an optional binding for |
297 |
| - the fiber wrapping the body. Returns the result of `body` if no error, |
298 |
| - or the result of `catch` if an error.`` |
299 |
| - [body catch] |
300 |
| - (let [[[err fib]] catch |
301 |
| - f (gensym) |
302 |
| - r (gensym)] |
303 |
| - ~(let [,f (,fiber/new (fn :try [] ,body) :ie) |
304 |
| - ,r (,resume ,f)] |
305 |
| - (if (,= (,fiber/status ,f) :error) |
306 |
| - (do (def ,err ,r) ,(if fib ~(def ,fib ,f)) ,;(tuple/slice catch 1)) |
307 |
| - ,r)))) |
308 |
| - |
309 | 293 | (defmacro protect
|
310 | 294 | `Evaluate expressions, while capturing any errors. Evaluates to a tuple
|
311 | 295 | of two elements. The first element is true if successful, false if an
|
|
352 | 336 | (tuple 'if $fi $fi ret))))))
|
353 | 337 | ret)
|
354 | 338 |
|
| 339 | +(defmacro try |
| 340 | + ``Try something and catch errors. `body` is any expression, |
| 341 | + and `catch` should be a form, the first element of which is a tuple. This tuple |
| 342 | + should contain a binding for errors and an optional binding for |
| 343 | + the fiber wrapping the body. Returns the result of `body` if no error, |
| 344 | + or the result of `catch` if an error.`` |
| 345 | + [body catch] |
| 346 | + (assert (and (not (empty? catch)) (indexed? (catch 0))) "the first element of `catch` must be a tuple or array") |
| 347 | + (let [[err fib] (catch 0) |
| 348 | + r (or err (gensym)) |
| 349 | + f (or fib (gensym))] |
| 350 | + ~(let [,f (,fiber/new (fn :try [] ,body) :ie) |
| 351 | + ,r (,resume ,f)] |
| 352 | + (if (,= (,fiber/status ,f) :error) |
| 353 | + (do ,;(tuple/slice catch 1)) |
| 354 | + ,r)))) |
| 355 | + |
355 | 356 | (defmacro with-syms
|
356 | 357 | "Evaluates `body` with each symbol in `syms` bound to a generated, unique symbol."
|
357 | 358 | [syms & body]
|
|
0 commit comments