Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Mirror based reflection and traditional reflection?

Some languages like Dart use mirror based reflection so, in simple terms, what is the difference between such implementation and traditional reflection as you see in C# or Java.

Update: I found this excellent (and somewhat quirky) video by Gilad Bracha on Mirror based reflection in Newspeak. http://www.hpi.uni-potsdam.de/hirschfeld/events/past/media/100105_Bracha_2010_LinguisticReflectionViaMirrors_HPI.mp4 (mirror stuff starts at 7:42)

like image 817
numan salati Avatar asked Aug 26 '12 17:08

numan salati


People also ask

What is difference between reflection and mirroring?

A mirror is a piece of furniture people have in their houses, usually the bathroom, it shows their reflection. An example is, every day I wake up and look in the mirror and see myself. Reflect/reflection is when you can see you self in something as the light is bouncing off the object and back at you.

What did you observe about your reflection in the mirror?

You look into the mirror and see your own face inside the mirror. What you see is a reflection of your face in the mirror. We also see reflections of other objects that are in front of the mirror. An image can be seen in the mirror because the light reflected from an object falls on the mirror and it is reflected.

Why do we see our reflection in a mirror but not on a brick wall?

The main difference between the brick wall and a mirror is the flatness of the surface. The morphology of the surface will determine whether you will observe specular or diffuse reflection.

What are mirrors in programming?

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


1 Answers

For many uses, I don't think mirrors will be that different than Java reflection. The most important thing understand about mirrors is that they decouple the reflection API from the standard object API, so instead of obj.getClass() you use reflect(obj). It's a seemingly small difference, but it gives you a few nice things:

  1. The object API isn't polluted, and there's no danger of breaking reflection by overriding a reflective method.
  2. You can potentially have different mirror systems. Say, one that doesn't allow access to private methods. This could end up being very useful for tools.
  3. The mirror system doesn't have to be included. For compiling to JS this can be important. If mirrors aren't used then there's no out-of-band to access code and pruning becomes viable.
  4. Mirrors can be made to work on remote code, not just local code, since you don't need the reflected object to be in the same Isolate or VM as the mirror.

Here's how mirrors are different than reflection in Java and Javascript when used to get an object's methods:

Java:

myObject.getClass().getMethods(); // returns an array

Dart:

reflect(myObject).type.methods; // returns a map

Javascript:

var methods = [];
for (var m in myObject) {
  if (typeof m === 'function') {
    methods.push(m);
  }
}
like image 106
Justin Fagnani Avatar answered Nov 14 '22 00:11

Justin Fagnani