Re: Readability of exception handling
Alan Baljeu <[email protected]> Mon, 7 Apr 2014 10:21:22 -0700 (PDT)
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
>> on_exception(Goal,
>> [ Term => { Goal },
>> ...
> >]).
>That syntax is fine too. It's nice and clear. I'm afraid I'm a bit out of my depth when it comes to defining new syntax.
Defining new syntax is utterly trivial. In this case, on_exception is just an ordinary predicate that takes a list of '=>'/2 terms.
on_exception(Goal, Handlers) :-
catch(Goal, Exception, process_exception(Handlers)).
process_exception(Exception, []).
process_exception(Exception, [H1 | Handlers]) :-
H1= (Term => {Code}),
Term = Exception
-> call(Code)
; process_exception(Terms).
The above code is probably not right, but it illustrates "new syntax". Specifically, it's just using built-in operators (or defining new ones) in place of ordinary terms.
-------------- next part --------------
HTML attachment scrubbed and removed