Cicada ---> Online Help Docs ---> Cicada scripting ---> Variables

This and that

At any point in a script there are several objects (variables or functions) that can be referenced using a built-in keyword. The first of these special keywords is ‘this’. In most examples from this chapter this refers to the workspace variable -- the top-level space where the user’s variables live. But when a function is running, ‘this’ refers to the function. For example, if we define the function


    f :: { int; this = args, return this }
   

and type f(5), our function will set its internal integer variable to 5 and return itself: { 5 }. In general ‘this’ is whatever object contains the code that is currently running.

Be warned that there are two situations where ‘this’ may not point where we expect. 1) If we use the this variable inside any curly braces, as in


    myVar :: {
       a := 2
       b := 5
       c := this[1]
    }
   

then it refers to what is inside the curly braces, not the workspace (so in this case c will be set to 2). 2) If ‘this’ appears inside the arguments of a function call, then it refers to any function arguments we have already passed. The reason is a bit more complicated and will be explained later, but for example if we write


    f(2, 5, this[2])
   

then we are effectively calling f(2, 5, 5). In some cases this can be annoying: for example if we want to print the number of workspace variables by typing


    print(top(this))   | won't work
   

we will instead print ‘1’ which is the number of arguments of the top() function. What we really want to do is back out 2 levels, first to the arguments of print() and then to the workspace, by typing


    print(top(parent.parent))
   

parent is the second predefined keyword, and refers to the object in the search path just before this. A shorthand for parent is a backslash character, so the last example could also have been written


    print(top(\.\))
   

A third keyword which appears only to the right of an assignment operator (= or <-) is called ‘that’. In its proper context, ‘that’ refers to whatever was on the left side of the assignment operator. It can be used to abbreviate cumbersome expressions such as


    facts.num.N = facts.num.N * log(facts.num.N) + facts.num.N
   

with


    facts.num.N = that * log(that) + that
   

The final keyword, called args, refers to a function’s arguments, which is almost always a composite variable. Functions are explained in more detail later, but here’s a quick example:


    I :: {
       code
       return { args, args[2] }
    }
   

If we type the command I(1, 2, 3), our function I will return { { 1, 2, 3 }, 2 }.


Prev: Resizing arrays (and composite variables)    Next: Aliases


Last update: May 8, 2024