Jim Newton
12/23/2015 2:56:00 PM
Common lisp has a type specifier operator CONS which can declare the car and cdr of a cons cell.
e.g.,
(defun F (x)
(declare (type (cons number null)) x)
; x is a singleton list of a number
...)
However, it is pretty common to have a list of homogeneous elements. E.g., a list of strings
or a list of symbols. But there's no easy way to declare this except excessive use of (cons...)
E.g., (cons number (cons number (cons number (cons number null)))) ; a list of 4 numbers.
My guess is that even if there were a way to make such a declaration, it would be of
dubious utility to the compiler or runtime environment.
Is it perhaps conceivable that the compiler or runtime environment could take advantage of
such a declaration given that cons cells are mutable.
Any function who receives a reference to a list may modify any cons cell in it.
It seems to me that there would be no way for the compiler to gain an advantage in such
a declaration.
Am I mistaken? Might there be some optimization a compiler could if the user could
declare the :element-type of a list? Would it be possible for the system to prevent
running code from breaking the declared promise via destructive functions?
Of course there might be advantages of readability for humans to understand the intent
of variables, but my question is about the compiler, or runtime environment.