Share on Facebook Share on Twitter Email
Answers.com

Type introspection

 
Wikipedia: 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.

Contents

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. To clarify, consider the following example code (you can immediately try this with irb):

$ 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
=> #<A:0x2e44b78>
irb(main):004:0> b=B.send 'new'
=> #<B:0x2e431b0>
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. In the example below the shows an alternative method in Ruby that can be used to define classes (and leads to the same result):

$ irb
irb(main):001:0> class A; end
=> nil
irb(main):002:0> class B < A; end
=> nil
irb(main):003:0> a=A.new
=> #<A:0x2e4c33c>
irb(main):004:0> b=B.new
=> #<B:0x2e4a974>
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

Or 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 <= A
=> 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 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++

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"
#include <boost/shared_ptr.hpp>
#include <map>
 
class TypeToTypedInstanceMap
{
public:
  template <class T> 
  void put(boost::shared_ptr<T> ptr)
  {
    map_[Loki::TypeInfo(typeid(T))] = ptr;
  }
 
  template <class T> 
  boost::shared_ptr<T> get()
  {
    return static_cast<boost::shared_ptr<T> >(map_[Loki::TypeInfo(typeid(T))]);
  }
private:
  std::map<Loki::TypeInfo, boost::shared_ptr<void> > map_;
};

Object Pascal

Type introspection has been a part of Object Pascal since the original release of Delphi, which uses RTTI heavily for visual form design. In Object Pascal, all classes descend from the base TObject class, which implements basic RTTI functionality. Every class's name can be referenced in code for RTTI purposes; the class name identifier is implemented as a pointer to the class's metadata, which can be declared and used as a variable of type TClass. The language includes an is operator, to determine if an object is or descends from a given class, an as operator, providing a type-checked typecast, and several TObject methods.

procedure Form1.MyButtonOnClick(Sender: TObject);
var
   aButton: TButton;
   SenderClass: TClass;
begin
   SenderClass := Sender.ClassType; //returns Sender's class pointer
   if sender is TButton then
   begin
      aButton := sender as TButton;
      EditBox.Text := aButton.Caption; //Property that the button has but generic objects don't
   end
   else begin
      EditBox.Text := Sender.ClassName; //returns the name of Sender's class as a string
   end;
end;


Java Object Introspection

The simplest example of type introspection in Java is the instanceof[1] operator. The instanceof operator determines whether a particular object belongs to a particular class (or a subclass of that class, or a class that implements that interface). For instance:

if(obj instanceof Person){
   Person p = (Person)obj;
   p.walk();
}

The java.lang.Class[2] class is the basis of more advanced introspection.

For instance, if it is desirable to determine the actual class of an object (rather than whether it is a member of a particular class), Object.getClass() and Class.getName() can be used:

System.out.println(obj.getClass().getName());


Perl

Introspection can be achieved using the ref and isa functions in Perl.

We can introspect the following classes and their corresponding instances:

package Animal;
sub new {
my($class) = shift;
   return bless {}, $class;
}
1;
package Dog;
use base Animal;
1;
my $animal = Animal->new();
my $dog = Dog->new();

using:

print 'This is an Animal.' if ref $animal eq 'Animal';
print 'Dog is an Animal.' if $dog->isa('Animal');

See also

References

  1. ^ Java Language Specification: instanceof
  2. ^ Java API: java.lang.Class

Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
 
 
Learn More
Kissing the Roof of Heaven (1990 Album by Hinterland)
Seconds Away (2003 Album by Glue)
Plural (1983 Album by Gal Costa)

What is an antonym for introspect? Read answer...
Introspective in a sentence? Read answer...
What is the Antonym of introspective? Read answer...

Help us answer these
What the synonym of introspectives?
What is introspective measure?
What is an introspective journal?

Post a question - any question - to the WikiAnswers community:

 

Copyrights:

Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Type introspection" Read more