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

StackOverflow https://stackoverflow.com/questions/19151569

  •  30-06-2022
  •  | 
  •  

문제

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
도움이 되었습니까?

해결책

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top