 in computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. some programming languages possess this capability. introspection should not be confused with reflection, which goes a step further and is the ability for a program to manipulate the values, meta-data, properties and/or functions of an object at runtime. some programming languages - e.g. java, python and go - also possess that capability. ==examples== ===ruby=== type introspection is a core feature of ruby. in ruby, the object class (ancestor of every class) provides object#instance_of? and object#kind_of? methods for checking the instance's class. the latter returns true when the particular instance the message was sent to is an instance of a descendant of the class in question. for example, consider the following example code (you can immediately try this with the interactive ruby shell): $ irb irb(main):001:0> a=class.new => a irb(main):002:0> b=class.new a => b irb(main):003:0> a=a.new => # irb(main):004:0> b=b.new => # irb(main):005:0> a.instance_of? a => true irb(main):006:0> b.instance_of? a => false irb(main):007:0> b.kind_of? a => true in the example above, the class class is used as any other class in ruby. two classes are created, a and b, the former is being a superclass of the latter, then one instance of each class is checked. the last expression gives true because a is a superclass of the class of b. further, you can directly ask for the class of any object, and "compare" them (code below assumes having executed the code above): irb(main):008:0> a.instance_of? class => true irb(main):009:0> a.class => a irb(main):010:0> a.class.class => class irb(main):011:0> a > b => true irb(main):012:0> b true ===objective-c=== in objective-c, for example, both the generic object and nsobject (in cocoa/openstep) provide the method ismemberofclass: which returns true if the argument to the method is an instance of the specified class. the method iskindofclass: analogously returns true if the argument inherits from the specified class. for example, say we have an apple and orange class inheriting from fruit. now, in the eat method we can write - eat: (id) to_eat { if([to_eat iskindofclass:[fruit class]]) { //we're actually eating a fruit, so continue if([to_eat ismemberofclass:[apple class]]) eat_apple(to_eat); else if([to_eat ismemberofclass:[orange class]]) eat_orange(to_eat); else error(); } else { error(); } } now, when eat is called with a generic object (an id), the function will behave correctly depending on the type of the generic objec
Object-oriented_programming