Equivalance of instanceof for CLOS? How to check if instance is inherited from another object?

故事扮演 提交于 2019-12-22 05:14:51

问题


CL-USER> (defclass a () ())
CL-USER> (defclass b (a) ())
CL-USER> (make-instance 'b)
#<STANDARD-CLASS B>

What predicate function can I call on my instance b, which returns T if it was inherited from a? In the vein of:

CL-USER> (instanceof 'a *)
T

回答1:


Class names are also type names, so:

(typep * 'a)

See Integrating Types and Classes: http://clhs.lisp.se/Body/04_cg.htm

Or you could do this:

(defmethod is-an-a-p ((x a))
  t)
(defmethod is-an-a-p ((x t))
  nil)


来源:https://stackoverflow.com/questions/19151569/equivalance-of-instanceof-for-clos-how-to-check-if-instance-is-inherited-from-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!