Class definition

Class is a set of variables and functions which describe properties and behaviour of a turtle (object) in POOL. It is a convenient way to organize a program which runs multiple turtles.

POOL uses the syntax of a function definition to define also a class. Body of the class-function contains code which is executed when new turtle is created. This code includes definitions of member functions and variables. Function which is used to define a class can be considered as a constructor of new turtle. Note: this function should not output any value.

Most of multi-turtle programming tasks will need a way of communication between turtles in order to exchange values of variables or mutually call member functions. POOL uses operator @ for this purpose.
All member functions in POOL are public and can be called mutually by turtle-objects. Variables are:
- public, if they are defined as global; public variables can be read with @ operator;
- protected, if they are defined as local; protected variables are not available for other turtles;
- static, if they are defined as shared; single instance of a shared variable is available for all turtles.

Example 1:

to model :p  ;constructor function for the class "model":
  make "l 0      ;global variable - public
  to fn :x       ;member function:
    make "l :x     ;save argument value in :l
    output :x^:p   ;result: power :p of the argument value
  end
end

;three turtles of the class "model" with different values of :p
make "t (list (newt $model 2) (newt $model 3) (newt $model 5))
print :t

;instruction "who" and function "fn :x" called for each turtle
foreach "z :t [(print who @ :z (fn repcount) @ :z)]

;value of :l variable of each turtle
foreach "z :t [print :l @ :z]

Output:

[model1 model2 model3]
model1 1
model2 8
model3 243
1
2
3

Example 2:

to model :h0 :c
  to step        ;member function
    rt :a fd :v
    "a := :a + rnorm 2
    if abs :a > 30 ["a := 30 * sign :a]
    "v := :v + rnorm 0.1
    if :v < 0 ["v := 0]
  end

  ;each turtle contains its own variables:
  make "v 3      ;public (global)
  let "a 0       ;protected (local)

  seth :h0       ;set initial heading
  setpc :c       ;set pen color
end

wrap
"n := 5
repeat :n [ ;create :n turtles of the class "model":
  "h := 360 * repcount / :n   ;turtle heading
  "c := 1000 * repcount / :n  ;turtle pen color
  "t := (newt $model :h :c)   ;...and the constructor of the turtle
]
ht

"ts := children ;list of turtles created by #first

;timer which is regularly calling "step" function:
let "tick timer [
  foreach "t :ts [step @ :t;call "step" for each turtle
  if :iter % 100 = 0 [       ;calculate the mean value of :v every 100 iteration
    let "s 0
    foreach "t :ts ["s := :s + :v @ :t;sum of :v of each turtle
    print :s / count :ts
  ]
] 10

Output:
(program is drawing in the graphics window)

3.60541860738304
3.24327855650685
3.35303447692073
2.92485334993107
3.17479251202894
3.43988973971224
...

See also:

Function definition and call
Remote variables and results of remote calls.
Veriables, data access

Turtle - object
#, turtle - get the turtle

Function as a value
$, func - get the function

Table of Content