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
·······@infa.abo.fi
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
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
Fredrik Sandstrom <·······@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
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" <······@acm.org> wrote in message
···················@vserver.cs.uit.no...
> Fredrik Sandstrom <·······@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