Static languages โ€‹โ€‹and reflection

As far as I know and understand

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

From Wikipedia:

  • Open and modify the construction of the source code (for example, code blocks, classes, methods, protocols, etc.) as a first-class object at runtime.
  • Converting a string corresponding to the symbolic name of a class or function into a link or calling this class or function.
  • Evaluate a string as if it were source code at runtime.
    Create a new interpreter for the language byte code to give a new meaning or purpose to the programming construct.

Dynamic languages are languages โ€‹โ€‹that we can eval (evaluate directly from a string) at runtime.

From Wikipedia: "expanding a program by adding new code by expanding objects and definitions or by changing the type system"

Ok, now my question is: How can a static language ื“ (without the ability to eval) be reflected (like Java)? (new source code not evaluating?: /)

Thanks in advance!

+6
source share
1 answer

I think the Wikipedia explanation is actually not very well formulated.

Reflection (or introspection) is not about creating code at runtime, but rather about the ability of the code to reflect itself at runtime (and modifying its behavior based on this). Examples are obtaining information about the type of an object (for example, RTTI in C ++) or obtaining metadata associated with an object (for example, annotations in Java).

Generating run-time code is more about first-class than reflection, so I would say that the current Wikipedia article is wrong.

UPDATE

In other words, answer this:

Ok, now my question is: how can a static language (without the ability of eval) have a reflection (like Java)?

... it would be that reflection is independent of evel's ability. These are two separate language features.

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

+4
source

Source: https://habr.com/ru/post/951410/


All Articles