Script variables and values

You can declare variables and values in your scripts. In order to do this, you can use a standard Scala syntax:

var x: Int = 3

A declaration expression is treated as a valid operand to a SubScript operator and is visible only to the operands of an operator it is declared under. For instance, the following two scripts are valid:

script first  = var x: Int = 3 ;  {x += 1} ; {println(x)}
script second = var x      = 3 ; [{x += 1} ; {println(x)}]

In the first script, first operand declares a variable x of type Int and assigns it a value of 3. Then in two separate code blocks it is incremented by 1 and printed correspondingly. All the operands belong to a single ; operator (note that in SubScript the majority of operators are n-ary (they accept indefinitely many operands), and the ; operator is also n-ary, not binary as it might seem).

In the second script, we also declare the variable x and assign it a value of 3, but this time we omit the type – it will be inferred by the compiler as in usual Scala code. All the operands in the parentheses will also have an access to x

Note that you must assign a value to a variable on creation. Also, you can’t define defs this way.

Leave a Reply