Julia – 58 – metaprogrammazione – 1

Continuo da qui, copio qui.

The strongest legacy of Lisp in the Julia language is its metaprogramming support. Like Lisp, Julia represents its own code as a data structure of the language itself. Since code is represented by objects that can be created and manipulated from within the language, it is possible for a program to transform and generate its own code. This allows sophisticated code generation without extra build steps, and also allows true Lisp-style macros operating at the level of abstract syntax trees. In contrast, preprocessor “macro” systems, like that of C and C++, perform textual manipulation and substitution before any actual parsing or interpretation occurs. Because all data types and code in Julia are represented by Julia data structures, powerful reflection capabilities are available to explore the internals of a program and its types just like any other data.

Rappresentazione del programma
Every Julia program starts life as a string:

What happens next?
The next step is to parse each string into an object called an expression, represented by the Julia type Expr:

Expr objects contain three parts:
1. a Symbol identifying the kind of expression. A symbol is an interned string identifier (more discussion below).

2. the expression arguments, which may be symbols, other expressions, or literal values:

3. finally, the expression result type, which may be annotated by the user or inferred by the compiler (and may be ignored completely for the purposes of this chapter):

Expressions may also be constructed directly in prefix notation:

The two expressions constructed above – by parsing and by direct construction – are equivalent:

The key point here is that Julia code is internally represented as a data structure that is accessible from the language itself.

The dump() function provides indented and annotated display of Expr objects:

Expr objects may also be nested:

Another way to view expressions is with Meta.show_sexpr, which displays the S-expression form of a given Expr, which may look very familiar to users of Lisp. Here’s an example illustrating the display on a nested Expr:

The : character has two syntactic purposes in Julia. The first form creates a Symbol, an interned string used as one building-block of expressions:

The Symbol constructor takes any number of arguments and creates a new symbol by concatenating their string representations together:

In the context of an expression, symbols are used to indicate access to variables; when an expression is evaluated, a symbol is replaced with the value bound to that symbol in the appropriate scope.

Sometimes extra parentheses around the argument to : are needed to avoid ambiguity in parsing.:

Prossimamente l’uso di tutto questo 😊


Posta un commento o usa questo indirizzo per il trackback.



Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: