ArticleslgStudy

science

Type introspection

Type introspection is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Type introspection rather than just read about it. In short: 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.

Key takeaways

  • Type introspection belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Type introspection to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Type introspection from memory before moving on to harder problems.

Reference excerpt

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 metadata, properties, and functions of an object at runtime. Some programming languages also possess that capability (e.g., Java, Python, Julia, and Go).

Examples

ActionScript In ActionScript (as3), the function flash.utils.getQualifiedClassName can be used to retrieve the class/type name of an arbitrary object.

Alternatively, the operator 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:

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 flash.utils.describeType function; this is used when implementing reflection in ActionScript.

Assemblers Some macro assemblers, e.g., the line from IBM Assembler F to IBM High Level Assembler (HLASM), allow a macro definition to examine the attributes of a symbol. This is a type of compile-time introspection.

C++ C++ supports type introspection via the run-time type information (RTTI) typeid and dynamic_cast keywords. The dynamic_cast expression can be used to determine whether a particular object is of a particular derived class. For instance:

The typeid operator retrieves a std::type_info object describing the most derived type of an object:

Using C++26's reflection, an object can be reflected with the ^^ operator (which produces a std::meta::info value, a mirror). This can then be checked against std::meta::type_of(), which returns the underlying type handle of a std::meta::info.

C# In C# introspection can be done using the is keyword. For instance:

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 an Orange class inheriting from Fruit. Now, in the eat method we can write

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

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 (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.

Java The simplest example of type introspection in Java is the instanceof 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:

The java.lang.Class 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:

PHP In PHP introspection can be done using instanceof operator. For instance:

Perl Introspection can be achieved using the ref and isa functions in Perl. We can introspect the following classes and their corresponding instances:

using:

Meta-Object Protocol Much more powerful introspection in Perl can be achieved using the Moose object system and the Class::MOP meta-object protocol; for example, you can check if a given object does a role X:

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:

Python The most common method of introspection in Python is using the dir function to detail the attributes of an object. For example:

Also, the built-in functions type and isinstance can be used to determine what an object is while hasattr can determine what an object does. For example:

Further, Python type hints can be accessed at runtime and can be used by libraries such as Pydantic for serialization and validation, and FastAPI for dependency injection and API schema generation.

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):

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):

Rust In Rust, there is a nightly API std::mem::type_info that provides compile-time type introspection. std::any::TypeId and std::mem::type_info::Type are a sort of mirror, allowing for limited compile-time reflection.

See also Reification (computer science) Reflective programming typeof

References

External links Introspection on Rosetta Code

Worked examples

Example 1 — a first encounter with Type introspection

Start with the simplest possible case. Write down what Type introspection claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Type introspection before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Type introspection ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Type introspection

In research
Type introspection appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Type introspection in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Type introspection is common in secondary-school and first-year university syllabi. It links to neighbouring topics Object-oriented programming, Programming language comparisons, so understanding it makes those chapters shorter.
In everyday life
Look for Type introspection outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Type introspection” →

How to study Type introspection in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Type introspection means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Type introspection out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Type introspection in simple terms?

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.

Why does Type introspection matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Type introspection?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Type introspection.

Tags

  • Object-oriented programming
  • Programming language comparisons

Keep exploring