Official ZPE/YASS documentationScope
The scope of a variable defines what can access it. The scope of a function defines what other functions can execute it. Scope is an important part of programming.
Public, friendly and private
ZPE/YASS defines three scopes, public, protected/friendly and private. These normally relate to how functions are defined within a structure and are not used outwith this situation.
||Anything within the program can access the variable or function.|
||Any function that is a descendant of the object, or that the object is a descendant of can access the variable or function.||Private||
||Only functions that are direct descendants of the object may access and/or modify the variable or execute the function.|
In the following example, a structure has been defined within the
main function also has a function,
defined within it. Since the
attempt1 function is a descendant of the
main function which defined the
person structure, it is
assumed that since the scope of the structure is protected, that the
function should be able to access it.
On the other hand, the
attempt2 function defined outwith the
function, and defined itself as a first-class citizen of the global function, should not
be able to access the
function main() structure person $name = null $age = 0 $email = null function person($n, $a, $e) $name = $n $age = $a $email = $e end function function _output() return $name & " " & $age & " " & $email end function end structure $campbell = new person("Campbell", 32, "firstname.lastname@example.org") print($campbell) function attempt1() $beth = new person("Beth", 93, "email@example.com") print($beth) end function attempt1() attempt2() end function function attempt2() //Calling this function will cause an issue because person was only defined within the main function $beth = new person("Beth", 93, "firstname.lastname@example.org") end function
Local and global scope
Since about version 1.6.8 of ZPE the concept of everything is a function and that functions should be treated as first-class citizens since version 1.3.7, the so called global function has been one of the most important concepts of ZPE.
If a variable is declared within a function, say for example, the
function, the scope of that variable is local and local only to the
function and any descendants (provided it has a public or protected scope).
If a function is defined in the global scope, the global function obtains that variable. All other functions are direct descendants of the global function so access to that variable is possible from every function, including those defined within objects.
ZPE/YASS supports defining variables outwith any functions to push them to the global scope but
it also has a
function which promotes a local variable to the global function.