MSE script has the notion of variables. A variable is a name holding a value, assigned using the := operator:

 variable := 1 + 1

From now on (until another value is assigned) variable evaluates to 2 in the rest of the script.


Assignments to variables are local to the current function. Consider:

 function := {
     x := "something else"
     # here x is something else
 x := 1
 # here x is still 1

Unlike most programming languages MSE script uses dynamic scoping. This means that assignments done in the calling function are visible in the called function:

 fun := { "xyz is {xyz}" }
 one := {
     xyz := 1
 two := {
     xyz := "two"
 one()  ==  "xyz is 1"
 two()  ==  "xyz is two"

This can be very useful for variables like card, which are used by many functions.

Overriding functions

Like custom functions, the built in functions are also stored in variables. It is possible to overwrite them:

 to_upper := { input }
 to_upper("xyz") == "xyz" # Not what it used to do

A neat trick is adding 'extra' behaviour to functions. This can be done by first making a copy, and calling that:

 real_to_upper := to_upper
 to_upper := { "upper case: " + real_to_upper() }
 to_upper("xyz") == "upper case: XYZ"

Note that real_to_upper is called without extra parameters, the input variable is still set from the outer call to the new to_upper itself.

previous: ← Operators | next: Functions →