Mirror (programming)
Appearance
In computer programming, a mirror is a reflection mechanism that is completely decoupled from the object whose structure is being introspected. This is as opposed to traditional reflection, for example in Java, where one introspects an object using methods from the object itself (e.g. java.lang.Object's getClass() method to get a java.lang.Class<?>).
Mirrors adhere to the qualities of encapsulation, stratification and ontological correspondence.[1]
Benefits
[edit]Decoupling the reflection mechanism from the objects themselves allows for a few benefits:
- The object's interface is not polluted, so there is no danger of breaking reflection by overriding a reflective method.
- There can be different mirror systems.
- The mirror system can be removed entirely (potentially allowing for optimizations).
- A mirror system can operate on remote code, since it is not coupled with a particular machine.
Languages that use mirrors
[edit]- C++, via the
^^operator and the returned fundamental typestd::meta::info.[2] - C#, via the
typeofoperator (see typeof) and its returnedSystem.Typeinstance, representing a type metadata. - Dart, via the
reflect()function. - Inko,[3] via its
std::mirrormodule. - Java, via
X.classwhich returns ajava.lang.Classinstance representing a type metadata; note that unlikejava.lang.Object'sgetClass()method, which returns a wildcardedjava.lang.Class<?>,X.classis ajava.lang.Class<X>. - Kotlin, via
X::classwhich returns akotlin.reflect.KClass<X>. Note that in Kotlin, a Java class metadata (java.lang.Class<X>) can be obtained withX::class.java. - Rubinius (Ruby implementation), via the
Rubinius::Mirror.reflect()method.[4] - Rust, via
std::mem::type_info::Type.[5] - Scala[6]
- Swift, via the
Swift.Mirrorclass and thereflect()function.
See also
[edit]References
[edit]- ↑ Gilad Bracha and David Ungar, "Meta-level Facilities of Object-Oriented Programming languages"
- ↑ cppreference.com. "std::meta::info". cppreference.com. Retrieved 19 May 2026.
- ↑ "Frequently Asked Questions".
- ↑ "Rubinius/Mirror.rb at master · rubinius/Rubinius".
- ↑ The Rust Team (8 September 2026). "Struct Type". doc.rust-lang.org. The Rust Team.
- ↑ "Environment, Universes, and Mirrors".