In Common Lisp I can check a type like:
(typep #(1 2 3) 'sequence)
;; returns T
And so I can use sequence in (declaim (ftype ... to specify a parameter of a function.
Is it possible to make the type even more specific and force a parameter to be a vector from fixnum?
Example:
(declaim (ftype (function (fixnum) sequence) abc))
(defun abc (n) #(n))
This function and its type definition works as expected. But can I define the return type more specific? Instead of sequence something like (vector fixnum)?
Possible type declarations for vectors
The Common Lisp HyperSpec provides an overview of type specifiers.
We can follow through the table there to the system class vector. This tells us that we can use
vectoras a Compound Type Specifier:Above is a syntax definition.
So declarations can use
vectortypes like:If you move the class chain upwards, you can see that the system class sequence does not provide such a syntax.
Note on returning a vector
If you write
(defun abc (n) #(n)), then it won't return the argument, but the symbol n. Elements of a literal vector are not evaluated.Above returns a literal vector of a symbol. literal also means that you should not modify it.
There is a backquote syntax for vectors:
We can also call the function
vector, which will create a vector with the argument values as its contents.