Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

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 protected $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.
The protected keyword was only added in ZPE 1.12.8, prior versions applied the protected scope when neither private nor public were specified.

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.

ZPE 1.14.2 also added private as a way of defining a local variable within a function definition, to avoid it conflicting with it's parent definitions.

Inner function using and modifying a variable from its containing function

YASS
// Everything here runs inside the implicit GLOBAL function

$x = 1
print("Global start: " & $x)

function outer()

  // This variable belongs to the OUTER function
  $counter = 10
  print("Outer start: " & $counter)

  function inner()

    // inner() can see and modify variables
    // from its containing (parent) function
    $counter = $counter + 1
    print("Inner changed counter to: " & $counter)

  end function

  inner()
  inner()

  // The change made by inner() persists here
  print("Outer end: " & $counter)

end function

outer()

// Global variable was never affected
print("Global end: " & $x)
Global start: 1
Outer start: 10
Inner changed counter to: 11
Inner changed counter to: 12
Outer end: 12
Global end: 1

Using private to keep a variable local to the inner function (version 1.14.2+)

YASS
function outer()

  // Variable belongs to the OUTER function
  $counter = 10
  print("Outer start: " & $counter)

  function inner()

    // This variable is PRIVATE (version 1.14.2 onwards) to inner()
    // It does not affect the outer counter
    private $counter = 0
    $counter = $counter + 1
    print("Inner private counter: " & $counter)

  end function

  inner()
  inner()

  // Outer counter is unchanged
  print("Outer end: " & $counter)

end function

outer()
Outer start: 10
Inner private counter: 1
Inner private counter: 1
Outer end: 10

Note, that using let does the same as private here, as of version 1.14.2.

Comments

There are no comments on this page.

New comment

Comments are welcome and encouraged, including disagreement and critique. However, this is not a space for abuse. Disagreement is welcome; personal attacks, harassment, or hate will be removed instantly. This site reflects personal opinions, not universal truths. If you can’t distinguish between the two, this probably isn’t the place for you. The system temporarily stores IP addresses and browser user agents for the purposes of spam prevention, moderation, and safeguarding. This data is automatically removed after fourteen days. Your email address is stored so that replies can be sent to your email address.

Comments powered by BalfComment

Feedback 👍
Comments are sent via email to me.