Type introspection
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, metadata, properties, and functions of an object at runtime. Some programming languages also possess that capability; e.g.,
Java,
Python,
Julia,
and
Go.
Examples
Ruby
Type introspection is a core feature of Ruby. In Ruby, the Object class provides and 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 :$ irb
irb:001:0> A=Class.new
=> A
irb:002:0> B=Class.new A
=> B
irb:003:0> a=A.new
=> #
irb:004:0> b=B.new
=> #
irb:005:0> a.instance_of? A
=> true
irb:006:0> b.instance_of? A
=> false
irb:007:0> b.kind_of? A
=> true
In the example above, the class is used as any other class in Ruby. Two classes are created, and, the former is being a superclass of the latter, then one instance of each class is checked. The last expression gives true because is a superclass of the class of.
Further, you can directly ask for the class of any object, and "compare" them :
irb:008:0> A.instance_of? Class
=> true
irb:009:0> a.class
=> A
irb:010:0> a.class.class
=> Class
irb:011:0> A > B
=> true
irb:012:0> B <= A
=> true
Objective-C
In Objective-C, for example, both the generic Object and NSObject provide the method which returns true if the argument to the method is an instance of the specified class. The method analogously returns true if the argument inherits from the specified class.For example, say we have an and an class inheriting from.
Now, in the method we can write
- eat:sth
Now, when is called with a generic object, the function will behave correctly depending on the type of the generic object.
C++
C++ supports type introspection via the run-time type information typeid and dynamic_cast keywords.The expression can be used to determine whether a particular object is of a particular derived class. For instance:
Person* p = dynamic_cast
if
The operator retrieves a object describing the most derived type of an object:
if typeid)
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. Deeper introspection is traditionally only supported for objects declared in the $M+ state, typically TPersistent, and only for symbols defined in the published section. Delphi 2010 increased this to nearly all symbols.
procedure Form1.MyButtonOnClick;
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
The simplest example of type introspection in Java is the operator. The operator determines whether a particular object belongs to a particular class. For instance:if
The class is the basis of more advanced introspection.
For instance, if it is desirable to determine the actual class of an object, and can be used:
System.out.println.getName);
PHP
In PHP introspection can be done using operator. For instance:if
Perl
Introspection can be achieved using the and functions in Perl.We can introspect the following classes and their corresponding instances:
package Animal;
sub new
package Dog;
use base 'Animal';
package main;
my $animal = Animal->new;
my $dog = Dog->new;
using:
print "This is an Animal.\n" if ref $animal eq 'Animal';
print "Dog is an Animal.\n" if $dog->isa;
Meta-Object Protocol
Much more powerful introspection in Perl can be achieved using the Moose object system and the meta-object protocol; for example, you can check if a given object does a role :if
This is how you can list fully qualified names of all of the methods that can be invoked on the object, together with the classes in which they were defined:
for my $method
Python
The most common method of introspection in Python is using the function to detail the attributes of an object. For example:class Foo:
def __init__:
self.x = val
def bar:
return self.x
>>> dir
Also, the built-in functions and can be used to determine what an object is while can determine what an object does. For example:
>>> a = Foo
>>> b = Bar
>>> type
>>> isinstance
True
>>> isinstance
True
>>> isinstance
False
>>> hasattr
True
In Python 2 but not Python 3, declaring instead of will result in returning the generic type instead of the class.
ActionScript (as3)
In ActionScript, the function
can be used to retrieve the class/type name of an arbitrary object.// all classes used in as3 must be imported explicitly
import flash.utils.getQualifiedClassName;
import flash.display.Sprite;
// trace is like System.out.println in Java or echo in PHP
trace; // "String"
trace; // "int", see dynamic casting for why not Number
trace)); // "flash.display.Sprite"
Alternatively, the operator
can be used to determine if an object is of a specific type:// trace is like System.out.println in Java or echo in PHP
trace; // true
trace; // false
trace; // false
trace; // true
This second function can be used to test class inheritance parents as well:
import flash.display.DisplayObject;
import flash.display.Sprite; // extends DisplayObject
trace; // true
trace; // true, because Sprite extends DisplayObject
trace; // false
Meta-Type introspection
Like Perl, ActionScript can go further than getting the class name, but all the metadata, functions and other elements that make up an object using the
function; this is used when implementing reflection in ActionScript.import flash.utils.describeType;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.display.Sprite;
var className:String = getQualifiedClassName); // "flash.display.Sprite"
var classRef:Class = getDefinitionByName; // Class reference to flash.displaySprite
// eg. 'new classRef' same as 'new flash.display.Sprite'
trace; // return XML object describing type
// same as : trace;