Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

conditions

2 views
Skip to first unread message

Fredrik Sandstrom

unread,
Aug 27, 2002, 8:06:15 AM8/27/02
to
I'm having problems getting the hang of the condition system...

Suppose I have the following form:
(destructuring-bind (a b c) list
...)

What I want to do is handle the error of LIST not matching the given
lambda list. The problem is, how do I know what error to look for?
CMUCL signals a condition of type
COMMON-LISP::DEFMACRO-LL-ARG-COUNT-ERROR which is helpful enough, but
CLISP just signals a SIMPLE-ERROR. Furthermore, I do not want to trap
errors that come from the body of the destructuring-bind form, only
errors with the actual binding of values to variables. How do I do this,
short of writing my own routine that checks if the list is OK?


--
Fredrik Sandström
fre...@infa.abo.fi

Paul F. Dietz

unread,
Aug 27, 2002, 8:33:37 AM8/27/02
to
Fredrik Sandstrom wrote:

> Suppose I have the following form:
> (destructuring-bind (a b c) list
> ...)
>
> What I want to do is handle the error of LIST not matching the given
> lambda list. The problem is, how do I know what error to look for?
> CMUCL signals a condition of type
> COMMON-LISP::DEFMACRO-LL-ARG-COUNT-ERROR which is helpful enough, but
> CLISP just signals a SIMPLE-ERROR. Furthermore, I do not want to trap
> errors that come from the body of the destructuring-bind form, only
> errors with the actual binding of values to variables. How do I do this,
> short of writing my own routine that checks if the list is OK?

The standard merely requires DESTRUCTURING-BIND to signal an ERROR
in this case; anything more detailed is implementation-specific.

You can avoid having the body's errors be caught by doing this:

(multiple-value-bind (a b c)
(handler-case (destructuring-bind (a b c) list
(values a b c))
;; error handling cases for destructuring-bind here
)
;; original body of destructuring-bind here
)

You can write a macro to generate this.

Paul

Frode Vatvedt Fjeld

unread,
Aug 27, 2002, 8:38:02 AM8/27/02
to
Fredrik Sandstrom <fre...@hal.sby.abo.fi> writes:

> I'm having problems getting the hang of the condition system...
>
> Suppose I have the following form:
> (destructuring-bind (a b c) list
> ...)
>
> What I want to do is handle the error of LIST not matching the given
> lambda list. The problem is, how do I know what error to look for?

According to CLHS, all you can depend upon is a condition of type
error, which won't tell you much. I think this means that d-bind is
only intended to be used when you believe you know the structure of
the list being destructured, and not as a matching or unification
probe. Then you have to write your own parser, or use some library.

> Furthermore, I do not want to trap errors that come from the body of
> the destructuring-bind form, only errors with the actual binding of
> values to variables.

That's not so difficult, just rewrite

(destructuring-bind (<lambda-list>) <form> <body>)

to something like

(multiple-value-bind (<lambda-variables>)
(handler-case
(destructuring-bind (<lambda-list>) <form>
(values <lambda-variables>))
...)
<body>)

--
Frode Vatvedt Fjeld

Paul F. Dietz

unread,
Aug 27, 2002, 8:54:07 AM8/27/02
to
I wrote:

> The standard merely requires DESTRUCTURING-BIND to signal an ERROR
> in this case; anything more detailed is implementation-specific.

More accurately, it only requires that the error be signaled in
safe code.

Paul

Will Fitzgerald

unread,
Aug 29, 2002, 4:33:24 AM8/29/02
to
Another thing to consider: The CLHS also specifies that DESTRUCTURING-BIND
only "should" signal an error, which means your code can be guaranteed to
catch an error when safety optimization is set to 3 (i.e., "safe code").

I think this is what is needed, then:

(multiple-value-bind (<lambda-variables>)
(handler-case
(destructuring-bind (<lambda-list>) <form>

(declare (optimize (safety 3)))


(values <lambda-variables>))
...)
<body>)


"Frode Vatvedt Fjeld" <fro...@acm.org> wrote in message
news:2hadn8a...@vserver.cs.uit.no...

0 new messages