[m-dev.] try syntax

Peter Wang novalazy at gmail.com
Thu Jan 22 17:10:03 AEDT 2009


Hi,

Catching exceptions is too cumbersome.  I think we should have built in
syntax, e.g. (adjust accordingly if the parser is too dumb)

    try [io(!IO)] (
	f(X, Y, Z, !IO)
    ) then (
        on_success(X, Y, Z)	% outputs in scope
    ) else (
        on_failure
    ) catch [_ : exc1] (	% zero or more catch blocks
        on_catch_exc1
    ) catch [E] (
        rethrow(exception(E))
    ) finally (			% optional
        cleanup
    )

io(!IO) selects try_io.
store(!S) selects try_store.
all(Solutions) selects try_all.
Otherwise use try.

We'd implement it by doing a tranformation to call the underlying
predicates in the exception module.  The following assumes no `finally'
block.

    try_io(
        (pred({X0, Y0, Z0}::out, !.IO::di, !:IO::uo) is det :-
            f(X0, Y0, Z0, !IO)
        ), TryResult, !IO),
    (
        TryResult = succeeded({X, Y, Z}),
        on_success(X, Y, Z)
    ;
        TryResult = failed,
        on_failure
    ;
        TryResult = exception(E0),
        ( univ_to_type(E0, _ : exc1) ->
            on_catch_exc1
        ; univ_to_type(E0, E) ->
            rethrow(exception(E))
        )
    )

Peter

--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions:          mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the developers mailing list