To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Official ZPE/YASS documentationGlobal scope

ZPE/YASS has had a global scope since a very early stage in its development. Whilst using global variables is not always very useful, there are times when they are needed.

The ZPE runtime defines a global function (known as globalFunction) that is:

  • The parent of all top-level functions in code
  • The parent of all global variables
  • The owner of all top-level structures

The following code sample demonstrate this:

YASS
function main()

  displayIt()
  $test1 = 10
  displayIt()

end function

function displayIt()

  print($test1)

end function

function getValue()

  return 2

end function

$test1 = getValue()

The $test1 variable is defined within the global scope since it is not defined within any function definition. The main and displayIt functions reference the $test1 variable within the global scope, not within their own scope. This means that the $test1 variable is changed within the global scope and not the local scope.

Notice as well, the $test1 variable is defined after the getValue function. If it were defined before this function, the runtime would have access to the function and would throw an error.

Everything as a function

The concept of first-class citizens has been available in ZPE/YASS since early days when lambda functions came. As a result, the move to the everything as a function initiative means that ZPE has gained higher performance as well as flexibility.

ZPE/YASS strives to ensure that the globalFunction - encompasing all other functions - allows for the same flexibility as other functions.

Other interpreters (if they ever exist) may not use a global scope as it's not actually defined in the formal specification for YASS. Further, the global scope may work totally differently and may not use a global function like ZPE/YASS does.

The global keyword

Prior to ZPE 1.10.12 (Zippy Zebra, December 2022), ZPE had the global function. This was used to promote a variable from a local variable to a global variable. ZPE 1.10.12 removed this and added the far more efficient and straightfoward global keyword. This actually uses the exact same syntax, so no changes needed to be made to code that previously used this:

YASS
global($x)
        

But the global keyword actually adds another functionality to this. That functionality is being able to declare a function inside another function, but declare the inner function as a global function:

YASS
function operation()
    global function innerFunction()
      print("This function is running")
    end function
  end if
end function
        
Comments

There are no comments on this page.

New comment
Reply
Code previewClose
Feedback 👍
Comments are sent via email to me.