Globala variabler och räckvidd för variabler

GEL är ett språk med dynamisk räckvidd. Vi kommer att förklara vad detta betyder nedan. Det betyder att normala variabler och funktioner har dynamisk räckvidd. Undantaget är parametervariabler som alltid är globala.

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.

Funktionsargument är exakt som variabler definierade i funktionen, förutom att de initieras med värdet som skickats till funktionen. Förutom denna punkt behandlas de precis som alla andra variabler som definieras i funktionen.

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)

Funktionen set ställer alltid in toppnivåglobalen. Det finns inget sätt att ställa in en lokal variabel i någon funktion från en subrutin. Om detta krävs måste du använda referensöverföring.

Se även funktionerna SetElement och SetVElement.

För att upprepa med mer tekniskt språk: Genius arbetar med olika numrerade kontexter. Toppnivån är kontext 0 (noll). Närhelst vi går in i en funktion höjs kontexten, och då funktionen returnerar sänks kontexten. En funktion eller en variabel är alltid synlig från alla kontexter med högre siffra. Då en variabel definierades i ett lägre kontextnummer, så har inställandet av denna variabel effekten att det skapar en ny lokal variabel i det aktuella kontextnumret och denna variabel kommer nu vara synlig från alla högre kontextnummer.

Det finns också verkligt lokala variabler som inte ses från någon annan plats än den aktuella kontexten. Vid returnering av funktioner efter värde kan det referera till variabler som ej är synliga från högre kontexter och detta kan vara ett problem. Se avsnitten Verkligt lokala variabler och Returnera funktioner.