At least some implementations of Common Lisp don't allow user-defined constants to be used as array dimensions in some type specifiers. For example, in SBCL, this code:
(defconstant +len+ 3)
(defun foo (x)
(declare (type (simple-array fixnum (+len+)) x))
x)
generates this error:
; in: DEFUN FOO
; (TYPE (SIMPLE-ARRAY FIXNUM (+LEN+)) X)
;
; caught ERROR:
; bad dimension in array type: +LEN+
Why? It seems surprising that user-defined constants can't be used in type specifiers, since it would be desirable to be able to coordinate multiple type specifiers using some kind of global definition. I understand that type specifiers need to be completely understandable at compile-time. But I would have thought that a compiler would be able to replace symbols defined with defconstant
by their literal values. I would have thought that this was one of the purposes of defconstant
. (I've been unsuccessful, so far, in getting deeper understanding of this issue from the Common Lisp Hyperspec, CLTL2, the SBCL manual, or what Google has turned up. I suspect the answer is there in some form ....)
(map '(simple-array fixnum (4)) #'1+ (the (simple-array fixnum (4)) arr))
, replacing the first 4 with a constant generates an error, but replacing the second 4 only generates a warning, and the correct result is returned. In SBCL, by contrast, both replacements cause errors. – Aeolis4
with a constant generates an error", it sounds like you mean you've replaced4
with the name of a constant, which is quite different from replacing4
with the value of a constant.defconstant
s aren't like C#define
s which do textual substitution. – Hemorrhoid