Understanding type specifiers in Common Lisp
Asked Answered
C

1

7

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?

Cognizable answered 8/8, 2013 at 7:42 Comment(1)
it means any integer equal or greater then zero (first is the lower bound second is the higher bound).Boone
R
5

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)
Rosebud answered 8/8, 2013 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.