Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static languages and Reflection

As far as i know and understand,

Reflection is the ability to create/modify source code at run time.

From Wikipedia:

  • Discover and modify source code constructions (such as code blocks, classes, methods, protocols, etc.) as a first-class object at runtime.
  • Convert a string matching the symbolic name of a class or function into a reference to or invocation of that class or function.
  • Evaluate a string as if it were a source code statement at runtime.
    Create a new interpreter for the language's bytecode to give a new
    meaning or purpose for a programming construct.

Dynamic languages are languages that we can eval (evaluate direct from string) at run-time.

From wikipedia: "extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system"

Ok, now, my question is: How static languageד (without eval ability) can have reflection (e.g Java)? (the new source code doesn't evaluate? :/)

Thanks in advance!

like image 426
gran33 Avatar asked Aug 09 '13 17:08

gran33


1 Answers

I think wikipedia explanation is not really phrased well.

Reflection (or introspection) is not about creation of code during runtime but rather about ability of code to reflect on itself in runtime (and modify it's behavior based on this). Examples would be getting info about type of the object (like RTTI in C++) or getting metadata associated with object (like annotations in Java).

Runtime code generation is more related to first-classness than to reflection, so I would argue that current wikipedia article is kind of misleading.

UPDATE

In other words answer to this:

Ok, now, my question is, how can static language (without eval ability) can have reflection (e.g Java)?

... would be that reflection does not rely on evel ability. These are 2 separate language features.

And also, as Giulio Franco pointed out in the comments - Java is capable of runtime code generation\manipulation (see CGLIB for example).

like image 138
Eugene Loy Avatar answered Sep 23 '22 14:09

Eugene Loy