Global variables

Global variables are accessible in all functions (nested and parent w.r.t function that created a variable), of a turtle. Note: all code out of any function is actually a body of the #first turtle's main function.

Turtles in POOL are executing their tasks concurrently, and each turtle has its own set of global and local variables. Turtles can read global variables of other turtles with use of @ operator. Global variables are equivalent to public variables known in other programming languages.

Example:

to class :a :b
  let "priv :a + :b  ;local variable - "protected"
  make "s2 :priv^2   ;global variable - "public"

  to get_s           ;getter for the protected variable
    op :priv
  end

  to set_s :s        ;setter for the protected variable
    make "priv :s
    make "s2 :s^2    ;can also do any other associated code
  end
end

make "t (newt $class 2 3)
print get_s @ :t
;print :priv @ :t    ;error: variable :priv is not accessible
print :s2 @ :t

(set_s 6) @ :t
print get_s @ :t
print :s2 @ :t

Output:

5
25
6
36

Zobacz także:

Data types
Variables, data access

Table of Content