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 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.

Scope Example Purpose
Public public $x = 10 Anything within the program can access the variable or function.
Protected/friendly $x = 10 Any function that is a descendant of the object, or that the object is a descendant of can access the variable or function.
Private private $x = 10 Only functions that are direct descendants of the object may access and/or modify the variable or execute the function.

Example

In the following example, a structure has been defined within the main function. The main function also has a function, attempt1, 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 attempt1 function should be able to access it.

On the other hand, the attempt2 function defined outwith the main function, and defined itself as a first-class citizen of the global function, should not be able to access the person structure.

YASS
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, "campbell123@123mail.com")

  print($campbell)

  function attempt1()

    $beth = new person("Beth", 93, "beth2k9@gmail.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, "beth2k9@gmail.com")

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 main function, the scope of that variable is local and local only to the main 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 global function which promotes a local variable to the global function.

Comments

There are no comments on this page.

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