InstanceOf

InstanceOf::class[object]
checks, if object was generated by means of class or a class derived directly or indirectly from class. The object must have been generated by New.
  • While TypeOf tests classes, InstanceOf tests objects.
This loads the package.
In[1]:=
Click for copyable input
The next cell generates a tiny class hierarchy. The class child demonstrates a typical usage of InstanceOf.
In[2]:=
Click for copyable input
We generate for each class an object by applying Dot to New and the class' constructor.
In[3]:=
Click for copyable input
As expected, obj and parent are accepted by f, because they do have base in their class hierarchy, while alien does not match.
In[4]:=
Click for copyable input
Out[4]=
The check can also be applied directly.
In[5]:=
Click for copyable input
Out[5]=
Any other "class" yields False.
In[6]:=
Click for copyable input
Out[6]=
Please observe that the ::-operator acts on the scond argument rather quickly. Therefore, the following fails.
In[7]:=
Click for copyable input
Out[7]=