-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Variable Resolution
Description and discussion of variable definition syntax and value resolution semantics, both for documentation purposes and future design discussions.
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.
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);