In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, a mirror is a
reflection Reflection or reflexion may refer to:
Science and technology
* Reflection (physics), a common wave phenomenon
** Specular reflection, reflection from a smooth surface
*** Mirror image, a reflection in a mirror or in water
** Signal reflection, in s ...
mechanism that is completely decoupled from the object whose structure is being introspected. This is as opposed to traditional reflection, for example in
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, where one introspects an object using methods from the object itself (e.g.
getClass()
).
Mirrors adhere to the qualities of
encapsulation
Encapsulation may refer to:
Chemistry
* Molecular encapsulation, in chemistry, the confinement of an individual molecule within a larger molecule
* Micro-encapsulation, in material science, the coating of microscopic particles with another mater ...
, stratification and ontological correspondence.
Benefits
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
*
Dart
Dart or DART may refer to:
* Dart, the equipment in the game of darts
Arts, entertainment and media
* Dart (comics), an Image Comics superhero
* Dart, a character from ''G.I. Joe''
* Dart, a ''Thomas & Friends'' railway engine character
* D ...
, via its ''reflect'' function.
*
Inko, via its ''std::mirror'' module.
*
Rubinius (
Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
implementation), via its ''Rubinius::Mirror.reflect'' method.
*
Scala
*
Swift
Swift or SWIFT most commonly refers to:
* SWIFT, an international organization facilitating transactions between banks
** SWIFT code
* Swift (programming language)
* Swift (bird), a family of birds
It may also refer to:
Organizations
* SWIFT ...
, via its ''reflect'' function.
References
Programming paradigms
{{comp-sci-stub