[Openmcl-devel] declare

Gary Byers gb at clozure.com
Fri Jun 5 04:42:23 UTC 2009



On Fri, 5 Jun 2009, slepstein at mindspring.com wrote:

> I believe in type checking,

Many people also believe in indentation, which would make your code
easier to read and make the problems in it more obvious.  It's real,
and worth believing in.

but the declare usage is puzzling me, and I think it has changed in the last 6 months or so?

Yes.

>
> What (I think) used to work and was acceptable in MCL:
> (defun foo-1 (x)
>    (declare (integer x y))

At the point where the declaration appears, there is no
binding of Y; the reference to Y in the DECLARE form
has nothing to do with the Y in the inner LET*.

MCL silently ignored malformed declarations, as did
CCL until fairly recently.  (I think that most of the
changes having to do with this are in the trunk and
not in the 1.3 release.)

The changes that Gail made to warn about malformed
or questionable declarations exposed several dozen
such malformed declarations in the CCL sources, and
fixing those cases makes the code at least somewhat
"better" (safer, faster, or both.)

>    (let* ((y 3))
>      (+ x y)))
> ;Compiler warnings :
> ;   In FOO-1: TYPE declaration for unknown variable Y
>
> This compiles without warnings:
> (defun foo-2 (x)
>    (declare (integer x))
>    (let* ((y 3))
>      (declare (integer y))
>      (+ x y)))

As it should: the declarations apply to variables which
are in scope (or entering scope) at the point where
the declarations appear.

>
> But this doesn?t:
> (defun foo-3 (x)
>    (declare (integer x))
>    (let* ((y 3))
>     	 (z (+ x y)))
>      (declare (integer y z))
>      (+ x y z)))
> Error: While compiling FOO-3 :
> DECLARE not expected in (DECLARE (INTEGER Y Z))., in process Listener(6).

You have a LET* form that binds a single variable Y, and then a call to
a function named Z (that you might have thought was a binding form for
a variable named Z, but your code is misparenthesized if so.  Declarations can
generally only meaningfully appear at the beginning of the body of the
containing form, before any executable forms.
> 
> Nor does this:
> (defun foo-4 (x)
>    (let* ((y 3))
>      (z (+ x y)))
>  (declare (integer x  y z))
>      (+ x y z))
>> Error: While compiling FOO-4 :
>>        DECLARE not expected in (DECLARE (INTEGER X Y Z))., in process Listener(6).
>
> It looks as if the declare has to come early, but how does one type z?

Actually, it comes too late, after the apparent call to a function named Z.

> Thanks for your help.
>

Most CL-aware text editors and IDEs have facilities to help with
paren-matching and indentation.

>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel



More information about the Openmcl-devel mailing list