Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding type specifiers in Common Lisp

I've found a great example of type checking in LispWorks Hyper Spec, but the "type specifier" link leads to a mere glossary not the denotation, and I got a little confused with the syntax.

In (check-type n (integer 0 *) "a positive integer") what does (integer 0 *) mean? I assume it means inclusive range from 0 to infinity, but is this so?

like image 730
akalenuk Avatar asked Aug 08 '13 07:08

akalenuk


1 Answers

Yes you can use type specifiers in common lisp, they can be very powerful if your compiler chooses to use them. While you may find uses for check-type, the most common kinds of type specifications come in the form of declarations.

The declare expression is not only used for types however it has a number of declaration identifiers and common lisp implementations are actually free to add their own.

The bit you are interested in though is 'types' and more specifically than that 'Type Specifiers'. That page will give you the low-down on a variety of ways to specify types, including the way you mentioned in your question.

Again be aware that your implementation doesn’t have to use the declarations it could just ignore them! Here is some more info on that.

And for some example code, here is the example that got me understanding the basics of how this works. Here and more here.

From the 4.2.3 Type Specifiers:

If a type specifier is a list, the car of the list is a symbol, and the rest of the list is subsidiary type information. Such a type specifier is called a compound type specifier. Except as explicitly stated otherwise, the subsidiary items can be unspecified. The unspecified subsidiary items are indicated by writing *. For example, to completely specify a vector, the type of the elements and the length of the vector must be present.

(vector double-float 100)

The following leaves the length unspecified:

(vector double-float *)

The following leaves the element type unspecified:

(vector * 100)
like image 55
Baggers Avatar answered Nov 05 '22 06:11

Baggers