理解Common Lisp中的类型说明符

7
我在LispWorks Hyper Spec中找到了一个很好的类型检查示例,但是“类型说明符”链接只是一个词汇表而不是定义,我对语法有点困惑。
(check-type n (integer 0 *) "a positive integer")中,(integer 0 *)是什么意思? 我假设它表示从0到无穷大的包括范围,但是确实是这样吗?

它意味着任何大于或等于零的整数(第一个是下限,第二个是上限)。 - user797257
1个回答

5

是的,你可以在Common Lisp中使用类型说明符,如果你的编译器选择使用它们,它们可以非常强大。虽然你可能会发现check-type有用,但最常见的类型说明符形式是声明

declare表达式不仅用于类型,还有许多声明标识符,而且Common Lisp实现实际上可以自由添加它们自己的声明标识符。

你感兴趣的部分是'types',更具体地说是'类型说明符'。那个页面将向你介绍各种指定类型的方法,包括你在问题中提到的方法。

请注意,您的实现不必使用这些声明,可以忽略它们!这里有更多相关信息。

以下是一个示例代码,让我了解了基本的工作原理。 这里 和更多 这里

来自 4.2.3 类型说明符

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接