68

Can anyone explain from a language/environment agnostic perspective the difference between these two notions?

Also is there a set of conditions that programming languages need to satisfy in order to be reflective and/or introspective?

And if there is, what are these conditions?

4
  • Just clicking through to the tag wiki of the tags you put on this question leads to en.wikipedia.org/wiki/Type_introspection, which is a pretty good starting point. No?
    – deceze
    Aug 8, 2014 at 7:51
  • It is, but it doesn't say anything about general conditions.
    – U r s u s
    Aug 8, 2014 at 8:13
  • 2
    Interestingly, this related question provides a very good answer: stackoverflow.com/questions/351577/…
    – slebetman
    Aug 8, 2014 at 8:31
  • Because most of the answers here are wrong, in my opinion, I will refer to the work that I think introduced/popularized the terms: Gilad Bracha, David M. Ungar: Mirrors: design principles for meta-level facilities of object-oriented programming languages. OOPSLA 2004: 331-344 Sep 8, 2021 at 8:11

3 Answers 3

61

The Wikipedia article has a pretty decent summary:

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, meta-data, properties and/or functions of an object at runtime. Some programming languages, e.g. Java, also possess that capability.

Take a statically typed, compiled program:

SomeType function foo(int i) {
    return new SomeType(i);
}

All the types are known and enforced at compile time, the program shouldn't even compile if it doesn't satisfy its own explicit type constraints. Dynamic programming languages don't typically exhibit this kind of rigidness, the types of variables aren't necessarily known at compile time. They may look more like this:

function foo(i) {
    return new SomeType(i);
}

The function can't guarantee what i is exactly, it's just passing it through. That may or may not cause problems somewhere, the type system can't help here. This kind of error checking is then typically relegated to userland code, for which such code needs introspection capabilities:

function foo(i) {
    if (!is_int(i)) {
        throw new InvalidArgumentException;
    }
    return new SomeType(i);
}

Where exactly to draw the line between introspection and reflection is somewhat debatable. One may say introspection is anything that allows code to test what something is ("What am I?"), whereas reflection is the ability to manipulate the program structure itself. For instance, a PHP example:

$ref = new ReflectionClass('Foo');
$foo = $ref->newInstanceWithoutConstructor();

The above code circumvents running the constructor of class Foo when creating a new instance of it. That's code manipulation at runtime. In practice though, the reflection API in PHP also contains introspection capabilities. Some of these capabilities are a duplicate of what can be done with "lower" introspection capabilities. E.g.:

$ref = new ReflectionClass($obj);
if ($ref->getName() == 'Foo') ...

if ($obj instanceof Foo) ...

Both snippets essentially do the same thing, but one uses reflection and the other what would be called introspection. As you see, there's hardly a clear dividing line. However, reflection is typically more powerful than introspection. For instance, in PHP you have to use the reflection API to get information about the types of arguments a function accepts. That's just "passive" introspection, but belongs to the reflection API. This is mostly a matter of practical implementation though.

In short, by the general definition, to be introspective a program needs to be able to examine parts of itself at runtime and execute different code based on this information. A reflective program beyond that can change its own code execution rules at runtime, for example opting not to invoke a constructor, which is otherwise a mandatory operation as defined by the language.

4
  • 4
    Introspection should not be confused with type introspection. Tcl for example has i/o introspection, function source introspection, runtime environment introspection (name of executable, library path, os, interpreter version etc.), function introspection (list all available functions), variable introspection (list all available variables) etc. but no type introspection since there is only one type in tcl: string.
    – slebetman
    Aug 8, 2014 at 8:37
  • Fair enough, changed my wording.
    – deceze
    Aug 8, 2014 at 8:39
  • Not the code, the object. Your answer implies that all statically typed programs have introspection built into them, but that isn't a case of the object looking at itself, but is one of the program / parser looking at the written commands and defining an object's attributes accordingly. The object itself is more or less passive throughout the process. For most of it, the object doesn't exist, as it hasn't been initialized yet. Even in languages w/ static types, introspection and reflection are both implemented via libraries and frameworks, and their apis.
    – Nate T
    Feb 23, 2022 at 2:53
  • An example of each: introspection in C with the GNOME GObject introspection library -- and reflection in Java via the reflection apis. Just putting them here for future readers. You still get my vote for a thorough and info-packed answer.
    – Nate T
    Feb 23, 2022 at 2:54
12

Reflection is a mechanism composed of two techniques :

  1. Introspection

    The ability for a program to examine itself

  2. Intercession

    The ability for a program to modify itself (his behaviour or his state)

Ref. https://fr.wikipedia.org/wiki/R%C3%A9flexion_(informatique)#Introspection_et_intercession

My reference is a french page because the english page doesn't refers directly to the term intercession.

2

Type introspection:

Ability of a programming language to examine the type or properties of an object at runtime.

Example (Java):

String myObj1 = new String();
MyCustomObject myObj2 = new MyCustomObject();
if(myObj2 instanceof MyCustomObject) {
    System.out.println("This is an example of type interospection");
}


Reflection:

Ability of a programming language to achieve below things at runtime.

  • Type introspect (as seen above)
  • Examine and modify a object.
  • and much more
3
  • How can reflection modify the class structure ? See here as well : stackoverflow.com/questions/1754084/…. Sep 18, 2017 at 18:41
  • Not the code, the object. Your answer implies that all statically typed programs have introspection built into them, but that isn't a case of the object looking at itself, but is one of the program / parser looking at the written commands and defining an object's attributes accordingly. The object itself is more or less passive throughout the process. For most of it, the object doesn't exist, as it hasn't been initialized yet. Even in languages w/ static types, introspection and reflection are both implemented via libraries and frameworks, and their apis.
    – Nate T
    Feb 23, 2022 at 2:48
  • An example of each: introspection in C with the GNOME GObject introspection library -- and reflection in Java via the reflection apis. You still get my vote for a thorough answer.
    – Nate T
    Feb 23, 2022 at 2:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.