Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an object is an instance of a given class or of a subclass of it

Is there an easy way to do this in Smalltalk? I'm 80% sure that there is some method but can't find it anywhere.

I know that I can use

(instance class = SomeClass) ifTrue:

And I know that I can use superclass etc... but I hope that there is something built in :)

like image 655
Uko Avatar asked Dec 08 '12 16:12

Uko


People also ask

How do you check if an object is an instance of a subclass?

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).

Is there a way to check if a class is a subclass of another class?

Python issubclass() is built-in function used to check if a class is a subclass of another class or not. This function returns True if the given class is the subclass of given class else it returns False . Return Type: True if object is subclass of a class, or any element of the tuple, otherwise False.

What method is used to check if an object is an instance of a certain class in Python?

Python isinstance() Function The isinstance() function returns True if the specified object is of the specified type, otherwise False .

How do you check if an object is of a certain class?

Use the instanceof operator to check if an object is an instance of a class, e.g. if (myObj instanceof MyClass) {} . The instanceof operator checks if the prototype property of the constructor appears in the prototype chain of the object and returns true if it does. Copied!


1 Answers

To test whether anObject is instance of aClass:

(anObject isMemberOf: aClass)

To test whether it is an instance of aClass or one of it subclasses:

(anObject isKindOf: aClass)
like image 175
aka.nice Avatar answered Oct 17 '22 05:10

aka.nice