Type introspection

In computing, type introspection is a capability of some object-oriented programming languages to determine the type of an object at runtime. This is a notable capability of the Objective-C language, and is a common feature in any language that allows object classes to be manipulated as first-class objects by the programmer. Type introspection can be used to implement polymorphism.



Type introspection is a core feature of Ruby as well. 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. To clarify, consider the following example code (you can immediately try this with irb):$ irbirb(main):001:0> A=Class.new=> Airb(main):002:0> B=Class.new A=> Birb(main):003:0> a=A.new=> #irb(main):004:0> b=B.send 'new'=> #irb(main):005:0> a.instance_of? A=> trueirb(main):006:0> b.instance_of? A=> falseirb(main):007:0> b.kind_of? A=> trueIn 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. In the example below the syntactic sugar provided by Ruby is used to define classes (and leads to the same result):$ irbirb(main):001:0> class A; end=> nilirb(main):002:0> class B < A; end=> nilirb(main):003:0> a=A.new=> #irb(main):004:0> b=B.new=> #irb(main):005:0> a.instance_of? A=> trueirb(main):006:0> b.instance_of? A=> falseirb(main):007:0> b.kind_of? A=> trueOr 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=> trueirb(main):009:0> a.class=> Airb(main):010:0> a.class.class=> Classirb(main):011:0> A > B=> trueirb(main):012:0> B <= A=> true


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 a Puppy and Kitten class inheriting from Animal, and a Vet class.

Now, in the desex method we can write- desex: (id) to_desex{ if( [to_desex isKindOfClass: [Animal class] ) { //we're actually desexing an Animal, so continue if( [to_desex isMemberOfClass: [Puppy class] ) desex_dog(to_desex); else if( [to_desex isMemberOfClass: [Kitten class] ) desex_cat(to_desex); else error(); } else { error();

Now, when desex is called with a generic object (an id), the function will behave correctly depending on the type of the generic object.


C++ supports type introspection via the typeid operator. To enable this operator, the code must be compiled with RTTI.

Andrei Alexandrescu showed how to wrap the typeid operator in a Equality Comparable and LessThan Comparable class in his book Modern C++ programming. Code is available in the Loki library.

#include "loki/TypeInfo.h"

class TypeToTypedInstanceMap{public: template void put(boost::shared_ptr ptr) { map_ [Loki::TypeInfo(typeid(T))] = ptr; }

template boost::shared_ptr get() { return static_cast >(map_ [Loki::TypeInfo(typeid(T))] ); }private: std::map > map_;};




ee also

*Reflection (computer science)
*Introspector (program)

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • introspection — [ ɛ̃trɔspɛksjɔ̃ ] n. f. • 1838; mot angl., du lat. introspicere « regarder à l intérieur » ♦ Psychol. Observation d une conscience individuelle par elle même. Se livrer, être porté à l introspection, à analyser ses états d âme, ses sentiments. La …   Encyclopédie Universelle

  • Type system — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • Introspection —  Pour l’article homonyme, voir Introspection (informatique).  Étymologiquement, le terme d introspection vient du latin « introspectus », action de regarder à l intérieur. En général, il désigne le fait, pour une conscience,… …   Wikipédia en Français

  • Introspection (informatique) — Réflexion (informatique) Pour les articles homonymes, voir Réflexion et Réflexivité. En programmation informatique, la réflexion est la capacité d un programme à examiner, et éventuellement à modifier, ses structures internes de haut niveau (par… …   Wikipédia en Français

  • Introspection (album) — Infobox Album | Name = Introspection Type = Studio Album Artist = Myriads Released = 2002 Recorded = Genre = Gothic metal Length = 74:05 Label = Napalm Records Producer = Reviews = Last album = In Spheres Without Time (1999) This album =… …   Wikipedia

  • Introspection (Greg Howe album) — Infobox Album | Name = Introspection Type = Album Artist = Greg Howe Released = Start date|1993 Recorded = Greg s home studio / Kevin Soffera s home studio Genre = Instrumental rock, jazz fusion Length = 41:44 Label = Shrapnel Producer = Greg… …   Wikipedia

  • Type physicalism — The relevant question: what will research discover? Can types of mental states be meaningfully described by types of physical events (type physicalism), or is there some other problem with this pursuit? Type physicalism (also known as reductive… …   Wikipedia

  • Type psychologique — Article principal : psychologie analytique. Les types psychologiques sont une typologie proposée par Carl Gustav Jung pour caractériser le mode de fonctionnement psychologique d un sujet. Elle aboutit à distinguer seize types psychologiques …   Wikipédia en Français

  • Myers-Briggs Type Indicator — Carl Jung in 1910. Myers and Briggs extrapolated their MBTI theory from Jung s writings in his book Psychological Types. The Myers Briggs Type Indicator (MBTI) assessment is a psychometric questionnaire designed to measure psychological… …   Wikipedia

  • Myers-Briggs Type Indicator — Psychologie Approches et courants Psychodynamique • Humani …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.