exprv4:tutorials:scope_and_set



Scope and set

Scope controls where a variable exists and which code can see it.

Most beginner scripts use normal assignment:

a = 10;

This creates or updates a normal variable.

The `set` keyword creates a local variable. Local variables are temporary and exist only inside the current block, function, or method scope.

if(true)
{
    set temp = 42;
};

Use `set` when a value is only needed temporarily.


Normal Assignment

Normal assignment uses `=`.

speed = 1200;
depth = 2.5;

At top level, normal assignment creates variables in the root/session scope.

These variables can be used by later statements in the same session.

a = 10;
b = 20;
 
a + b;

The final value is `30`.


set Creates a Local Variable

Use `set` to create a local variable.

a = 10;
 
if(true)
{
    set temp = 5;
    a + temp;
};

The local variable `temp` exists inside the block.

Outside the block, `temp` is no longer available.

if(true)
{
    set temp = 5;
};
 
temp;

The final value is `none()` because `temp` does not exist outside the block.


set Is Not Used at Top Level

Do not use `set` for normal top-level variables.

Use this:

speed = 1200;

Not this:

set speed = 1200;    // not for top-level variables

At top level, use normal assignment with `=`.


Local Variables Can Shadow Outer Variables

A local variable can have the same name as an outer variable.

a = 5;
 
if(true)
{
    set a = 10;
    a;
};
 
a;

Inside the block, `a` is `10`.

After the block, the outer `a` is still `5`.

This is called shadowing. The local variable hides the outer variable while you are inside the local scope.


Updating a Local Variable

After a local variable is created with `set`, normal assignment updates that local variable while it is in scope.

a = 1;
 
if(true)
{
    set a = 2;
    a += 3;
    a;
};
 
a;

Inside the block, local `a` becomes `5`.

After the block, the outer `a` is still `1`.


Plain Assignment Can Update an Outer Variable

If you do not use `set`, assignment looks for an existing variable and updates it.

a = 5;
 
if(true)
{
    a = 2;
};
 
a;

The final value is `2`.

The block did not create a local `a`, so assignment updated the existing outer variable.


Plain Assignment Can Create a Root Variable

If no matching local variable exists, normal assignment can create a root/session variable.

if(true)
{
    b = 3;
};
 
b;

The final value is `3`.

This is useful, but it can also create accidental variables if you mistype a name. Use clear names and keep scripts small enough to inspect.


Function Parameters Are Local

Function parameters are local to the function call.

a = 100;
 
function AddOne(a)
{
    return a + 1;
}
 
result = AddOne(5);
 
a;

The final value is `100`.

The parameter `a` inside `AddOne` does not overwrite the outer variable `a`.


set Inside Functions

Use `set` inside functions for temporary values.

function ToolArea(diameter)
{
    set radius = diameter / 2;
    return pi() * radius ** 2;
}
 
ToolArea(10);

The local variable `radius` only exists while the function is running.

This keeps helper values from leaking into the rest of the script.


Assignment Inside Functions

Plain assignment inside a function can update an existing variable or create a root/session variable if no local variable matches.

function SaveLastTool(toolNumber)
{
    LastTool = toolNumber;
}
 
SaveLastTool(3);
LastTool;

The final value is `3`.

This can be useful when a function intentionally updates shared script state.

When you only need a temporary value, use `set` instead.


Object Fields and set

Inside class constructors and methods, normal assignment is used for object fields.

class Counter()
{
    Value = 0;
 
    function Inc()
    {
        Value += 1;
        return Value;
    }
}
 
counter = Counter();
counter.Inc();
counter.Value;

The final value is `1`.

Inside object methods, `Value += 1;` updates the object field `Value`.

Use `set` inside methods only for temporary local values.

class Counter()
{
    Value = 10;
 
    function PreviewNext()
    {
        set Value = this.Value + 1;
        return Value;
    }
}
 
counter = Counter();
preview = counter.PreviewNext();
actual = counter.Value;
 
preview * 100 + actual;

The final value is `1110` because `preview` is `11` and the stored object field is still `10`.

The local `set Value` shadows the object field inside the method.


Object Fields Do Not Overwrite Same-Named Root Variables

Inside object context, assignment prefers object fields over root/session variables.

Value = 100;
 
class Counter()
{
    function Set()
    {
        Value = 5;
    }
}
 
counter = Counter();
counter.Set();
 
Value;

The final value is `100`.

The assignment inside `Set()` creates or updates the object field `counter.Value`. It does not overwrite the root variable `Value`.

counter.Value;

This value is `5`.


Practical Rule

Use normal assignment `=` when:

  • You are creating or updating a top-level/root variable.
  • You intentionally want a function to update shared state.
  • You are creating or updating object fields inside a class or method.

Use `set` when:

  • A value is temporary.
  • A helper variable should not leak outside a block or function.
  • You want to avoid accidentally changing an outer variable.

Common Mistakes

Expecting set To Persist

A local variable disappears when its scope ends.

if(true)
{
    set temp = 5;
};
 
temp;       // none()

Accidentally Updating an Outer Variable

This updates the outer variable:

a = 1;
 
if(true)
{
    a = 2;
};

Use `set` if you wanted a temporary local value:

a = 1;
 
if(true)
{
    set a = 2;
};

Shadowing Object Fields

Inside a method, `set Value = …;` creates a local variable. It does not update the object field.

Use assignment without `set` when updating object state:

Value = Value + 1;

Try This

Read this script and predict the final value:

a = 10;
 
function Test(a)
{
    set b = a + 5;
    return b;
}
 
result = Test(3);
 
a * 100 + result;

The final value is `1008` because the outer `a` remains `10`, and `result` is `8`.

Next: Classes and objects

exprv4/tutorials/scope_and_set.txt · Last modified: by 127.0.0.1

Page Tools