Menu

Global variables

Mark Anthony Taylor (Shyreman)

Global variables are frowned upon, as breaking the OO paradigm, but they are convenient, so are given a minimal implementation.

At the top level in a sexy-file use

(global <type> <name> = <default-value>)

to create a global variable of specified type, name and default value.

An example is:

(global Int32 highScore = 2000)

Global variables are global in that their definition lies outside of functions, structures and classes.
They can only be accessed in the module in which they are defined by use of the (global ...) directive:

(function GetHighScore -> (float value):
    (global highScore -> value) // Notice the direction of the arrow towards the local variable
)

(global <global-name> -> <local-name>) assigns the value of the global variable to the local variable.
(global <global-name> <- <local-name>) assigns the value of the local variable to the global variable.

Here we assign a variable value to the global variable thus:

(function SetHighScore (float value)-> :
    (global highScore <- value) // Notice the direction of the arrow towards the global variable
)

One can also assign a constant numeric literal to the global variable:

(function SetHighScoreToZero -> :
    (global highScore <- 0) 
)

Currently only primitive types can be used as global variables. Global variable names cannot be aliased
into a namespace, therefore alias get and set functions to expose the global values outside the module
in which the global variables defined.


Related

Wiki: Content

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.