In computing, type introspection is the ability of a program to examine the type or properties of an objectat 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 metadata, properties, and functions of an object at runtime. Some programming languages also possess that capability (e.g.,Java,Python,Julia,andGo).
In Objective-C, for example, both the generic Object and NSObject (in Cocoa/OpenStep) 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
Now, when is called with a generic object (an), the function will behave correctly depending on the type of the generic object.
C++ supports type introspection via the run-time type information (RTTI) typeid and dynamic_cast keywords.The expression can be used to determine whether a particular object is of a particular derived class. For instance:
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 (enumerating fields and methods) is traditionally only supported for objects declared in the $M+ (a pragma) state, typically TPersistent, and only for symbols defined in the published section. Delphi 2010 increased this to nearly all symbols.
The simplest example of type introspection in Java is the [1] operator. The 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:
The [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), and can be used:
In PHP introspection can be done using operator. For instance:
Introspection can be achieved using the and functions in Perl.
We can introspect the following classes and their corresponding instances:
package Dog;use base 'Animal';
package main;my $animal = Animal->new;my $dog = Dog->new;using:
Much more powerful introspection in Perl can be achieved using the Moose object system[3] and the meta-object protocol;[4] for example, you can check if a given object does a role :
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:
The most common method of introspection in Python is using the function to detail the attributes of an object. For example:
def bar(self): return self.x
Also, the built-in functions and can be used to determine what an object is while can determine what an object does. For example:
Type introspection is a core feature of Ruby. In Ruby, the Object class (ancestor of every 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 (you can immediately try this with the Interactive Ruby Shell):
Further, you can directly ask for the class of any object, and "compare" them (code below assumes having executed the code above):
In ActionScript (as3), the function [http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getQualifiedClassName%28%29 flash.utils.getQualifiedClassName]
can be used to retrieve the class/type name of an arbitrary object.
Alternatively, the operator [http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#is is]
can be used to determine if an object is of a specific type:
This second function can be used to test class inheritance parents as well:
trace(new flash.display.Sprite is flash.display.Sprite); // truetrace(new flash.display.Sprite is flash.display.DisplayObject); // true, because Sprite extends DisplayObjecttrace(new flash.display.Sprite is String); // false
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 [http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#describeType%28%29 flash.utils.describeType]
function; this is used when implementing reflection in ActionScript.
var className:String = getQualifiedClassName(new flash.display.Sprite); // "flash.display.Sprite"var classRef:Class = getDefinitionByName(className); // Class reference to flash.displaySprite// eg. 'new classRef' same as 'new flash.display.Sprite'trace(describeType(classRef)); // return XML object describing type// same as : trace(describeType(flash.display.Sprite));