Concept

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). 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): $ 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 => # irb(main):004:0> b=B.new => # 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 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 (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 true 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 .

About this result
This page is automatically generated and may contain information that is not correct, complete, up-to-date, or relevant to your search query. The same applies to every other page on this website. Please make sure to verify the information with EPFL's official sources.

Graph Chatbot

Chat with Graph Search

Ask any question about EPFL courses, lectures, exercises, research, news, etc. or try the example questions below.

DISCLAIMER: The Graph Chatbot is not programmed to provide explicit or categorical answers to your questions. Rather, it transforms your questions into API requests that are distributed across the various IT services officially administered by EPFL. Its purpose is solely to collect and recommend relevant references to content that you can explore to help you answer your questions.