GEL je jazyk s dynamickým rozsahem platnosti. Co to znamená hned vysvětlíme. Je to to, že normální proměnné a funkce mají dynamicky vymezenou platnost. Výjimkou jsou proměnné parametrů, kterou jsou vždy globální.
Like most programming languages, GEL has different types
of variables. Normally when a variable is defined in a function,
it is visible from that function and from all functions that are
called (all higher contexts). For example, suppose a function
f
defines a variable a
and then calls function g
. Then
function g
can reference
a
. But once f
returns,
the variable a
goes out of scope.
For example, the following code will print out 5.
The function g
cannot be called on the
top level (outside f
as a
will not be defined).
function f() = (a:=5; g());
function g() = print(a);
f();
If you define a variable inside a function it will override any variables defined in calling functions. For example, we modify the above code and write:
function f() = (a:=5; g());
function g() = print(a);
a:=10;
f();
This code will still print out 5. But if you call
g
outside of f
then
you will get a printout of 10. Note that
setting a
to 5 inside f
does not change
the value of a
at the top (global) level,
so if you now check the value of a
it will
still be 10.
Argumenty funkce jsou úplně stejné jako proměnné definované uvnitř funkce vyjma toho, že jsou inicializovány na hodnotu, která je funkci předána. Kromě této jediné věci se s nimi zachází úplně stejně, jako se všemi ostatními proměnnými definovanými uvnitř funkce.
Functions are treated exactly like variables. Hence you can locally redefine functions. Normally (on the top level) you cannot redefine protected variables and functions. But locally you can do this. Consider the following session:
genius>
function f(x) = sin(x)^2
= (`(x)=(sin(x)^2))
genius>
function f(x) = sin(x)^2
= (`(x)=(sin(x)^2))
genius>
function g(x) = ((function sin(x)=x^10);f(x))
= (`(x)=((sin:=(`(x)=(x^10)));f(x)))
genius>
g(10)
= 1e20
Functions and variables defined at the top level are
considered global. They are visible from anywhere. As we
said the following function f
will not change the value of a
to 5.
a=6;
function f() = (a:=5);
f();
Sometimes, however, it is necessary to set
a global variable from inside a function. When this behavior is needed,
use the
set
function. Passing a string or a quoted identifier to
this function sets the variable globally (on the top level).
For example, to set
a
to the value 3 you could call:
set(`a,3)
or:
set("a",3)
Funkce set
nastavuje vždy globální proměnné v nejvyšší úrovni. Neexistuje žádný způsob, jak nastavit lokální proměnnou v nějaké funkce z podřízené funkce. Pokud něco takového potřebujete, musíte jedině použít předání reference (odkazu).
Viz také funkce SetElement
a SetVElement
.
Takže sesumírováno do technického jazyka: Genius pracuje s různými očíslovanými kontexty. Nejvyšší úroveň je kontext 0 (nula). Kdykoliv se vstoupí do funkce, je kontext zvýšen a když se funkce opouští, je kontext snížen. Funkce nebo proměnná je vždy viditelná ze všech kontextů, které mají vyšší číslo. Když byla proměnná definována v kontextu s nižším číslem, má nastavení této proměnné vliv na vytváření nové lokální proměnné v aktuálním čísle kontextu a tato proměnná bude nyní viditelná ze všech kontextů s vyšším číslem.
Existují i skutečně lokální proměnné, které nejsou vidět nikde jinde, než v aktuálním kontextu. Rovněž při vracení funkcí hodnotou je možné odkazovat na proměnnou, která není viditelná z vyššího kontextu a to může být problém. Viz oddíl Skutečně lokální proměnné a Vracení funkcí.