问题描述
Greg Pfeil的图提供了通用的全面图Lisp类型系统。但是,我试图更好地理解层次结构顶部的类关系。举一个简单的例子,让(破坏人的名字年龄)
,然后让(defparameter * p1 *(make-person:name Yosh:age 19)
。现在
Greg Pfeil's Class Hierarchy diagram provides a comprehensive picture the Common Lisp type system. But I'm trying to better understand the class relationships at the top of the hierarchy. For a simple example, let (defstruct person name age)
, and then (defparameter *p1* (make-person :name "Yosh" :age 19)
. Now
(typep *p1* 'person)
T
(typep *p1* 'structure)
T
(typep *p1* 'structure-object)
T
(typep *p1* 'atom)
T
(typep *p1* t)
T
-
表示
结构对象
本身就是t
。
是atom
和结构
不是层次结构中的类型?
The Hyperspec says the precedence list for
structure-object
is only itself andt
.Areatom
andstructure
not types in the hierarchy?
的所有直接子类型是什么code> t ?更一般地说,如何检索任何给定类型的所有直接子类型或超类型(没有反复试验的 subtypep
)?或者,是否有一种方法可以遍历所有类型的列表?MOP是否提供函数来获取所有子类-/ super-classes?
What are all the direct subtypes of t
? More generally, how can you retrieve all the direct subtypes or supertypes of any given type (without trial-and-error subtypep
)? Alternately, is there a way to iterate over a list of all types? Does the MOP offer functions to get at all sub-/super-classes?
与集合论类似,从理论上讲,似乎所有Common Lisp类型/类都可以细分为<$的两个子类。 c $ c> t ;即 standard-object
(与 elements 相对应,其实例为数字3,字符串 abc,结构s1,方法m1,等等)和 standard-class
(与 sets 对应的类 standard-object $ c这样的实例) $ c>,类
number
,类 structure-object
等)。如果这不是 t
的实际细分,那么原因是否与实际实现有关?例如,避免层次结构中的递归类关系?
By analogy with set theory, it would seem that all Common Lisp types/classes could theoretically be subdivided into two subclasses of t
; namely, standard-object
(corresponding to elements with instances like the number 3, the string "abc", the structure s1, the method m1, etc.), and standard-class
(corresponding to sets with instances like the class standard-object
, the class number
, the class structure-object
, etc.). If this is not the actual subdivision of t
, does the reason have something to do with practical implementation; for example, avoiding recursive class relationships in the hierarchy?
推荐答案
类型和类是两回事。
不要混淆它们。
有些类型具有相应的类。大多数人没有。
Some types have corresponding classes. Most have not.
atom
是类型的名称,而不是类的名称。
atom
is the name of a type, but not of a class.
CL-USER 18 > (find-class 'atom nil)
NIL
因为 atom
不是类,它不能在任何类优先级列表中。
因此, atom
不在 structure-object
的类优先级列表中。
Since atom
is not a class, it can't be in any class precedence list.Thus atom
is not in the class precedence list of structure-object
.
类型结构
是非标准的,不是ANSI CL定义的。
The type structure
is non-standard and not defined by ANSI CL.
类型不在类优先级列表中,而是类。
Types are not in a class precedence list, classes are.
类型的接口:
- 创建类型->
DEFTYPE
- 是某种类型的东西吗? ->
TYPEP
- 一种类型是另一种类型的子类型吗? ->
SUBTYPEP
- 什么是某种类型? ->
TYPE-OF
- create a type ->
DEFTYPE
- is something of a type? ->
TYPEP
- is a type a subtype of another type? ->
SUBTYPEP
- what is a type for something? ->
TYPE-OF
基本上,您可以使用类型进行所有操作。
That's basically all you can do with types.
CLOS类具有相应的类型
CLOS函数和类优先级列表不
CLOS functions and class precedence lists don't work with types, but classes have corresponding types.
CL-USER 23 > (defclass bar () ())
#<STANDARD-CLASS BAR 40200A2413>
CL-USER 24 > (typep (make-instance 'bar) 'bar)
T
CL-USER 25 > (type-of (make-instance 'bar))
BAR
CL-USER 26 > (class-of (make-instance 'bar))
#<STANDARD-CLASS BAR 40200A2413>
CLOS适用于类。因此,在扩展的CLOS中,您可以要求子类和超类。但是不适用于子类型或超类型。
CLOS works with classes. Thus in an extended CLOS you can ask for subclasses and superclasses. But not for subtypes or supertypes.
历史记录:类型和CLOS
通用Lisp从CLtL1开始,没有类型,没有CLOS。
Common Lisp started in CLtL1 with types and no CLOS.
CLOS和CLOS类已在几年后添加。添加它们的方式是一些类型获得了相应的类,并且这些类具有了相应的类型。
CLOS and CLOS classes have been added years later. They have been added in such a way that some types got corresponding classes and such that classes have corresponding types.
Common Lisp允许定义类型使用类型说明符,例如 AND
, OR
, SATISFIES
,成员
,否
,...对于那些不存在相应的CLOS类的情况。
Common Lisp allows to define types using type specifiers like AND
, OR
, SATISFIES
, MEMBER
, NOT
, ... For those no corresponding CLOS classes exist.
也有复合类型说明符,例如(整数0 100)
。这些类型也没有相应的CLOS类。
There are also compound type specifiers like (integer 0 100)
. There are also no corresponding CLOS classes for those types.
CL-USER 31 > (deftype integer100 () '(integer 0 100))
INTEGER100
CL-USER 32 > (find-class 'integer100)
Error: INTEGER100 is not the name of a class
这篇关于通用Lisp类层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!