Skip to content

Variable Resolution

Marius Kintel edited this page Mar 28, 2016 · 3 revisions

Variable Resolution

Description and discussion of variable definition syntax and value resolution semantics, both for documentation purposes and future design discussions.

Classic OpenSCAD resolution semantics

OpenSCAD has "weird" variable resolution semantics. The weirdness of these semantics are unindended and arise from an early hack, allowing variable overrides on the cmd-line and is since kept for backwards compatibility reasons.

Variables in the global scope

Hoisting:

Variables are hoisted to the top of the main file. This will echo "23":

    echo(a);
    a = 23;

Overrides

An existing variables can be overridden by assigning it another value. Since variable are hoisted, the new value will be the only visible value. The following will echo "1" twice:

   a = 1;
   echo(a);
   a = 2;
   echo(a);

Dependent variables:

Variables depending on other variables are evaluated in the order they're defined. You cannot refer to a variable defined later. E.g. this won't work:

   b = a*3;   // Forward references are not allowed
   a = 1;

This also means that a variable cannot reference itself. This also won't work:

    a = 0;     // This will be replaced by the overridden definition and the value 0 thus won't exist.
    a = a+1;

Overriding dependent variables:

This is a bit weird: When overriding a variable, it will be be moved to the position of the originally defined variable. This will echo "2,6"

    a=1;
    b=a*3;
    a=2;
    echo(a,b);

Clone this wiki locally