Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 245
mck Avatar asked Oct 03 '13 05:10

mck


People also ask

Which operator is used to check if the object of a class is inheriting a property from another class?

The instanceof operator allows to check whether an object belongs to a certain class. It also takes inheritance into account.

How do I check if an object is an instance of a given class or of a subclass of it?

The isinstance() method checks whether an object is an instance of a class whereas issubclass() method asks whether one class is a subclass of another class (or other classes).

Does Instanceof check superclass?

Using instanceof operator, when a class extends a concrete class. When a class extends a concrete class, the instanceof operator returns true when a subclass object is checked against its concrete superclass-type.

How do you check if something is an instance of a class?

Java instanceof Operator The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .


1 Answers

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)
like image 197
Lars Brinkhoff Avatar answered Sep 22 '22 18:09

Lars Brinkhoff