File: function-scope.scad

package info (click to toggle)
openscad 2021.01-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 36,096 kB
  • sloc: cpp: 53,199; sh: 4,384; ansic: 4,382; python: 1,813; yacc: 853; javascript: 762; lex: 417; lisp: 163; xml: 127; makefile: 118
file content (15 lines) | stat: -rw-r--r-- 685 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Each 'let' creates a new context, allowing variable override
function duplicate_let() = let(x=42) let(x=33) x;
echo(duplicate_let=duplicate_let()); // 33

// Default value (x=42) must be used, if not given in call
function defaults(b, x=42) = b ? x : defaults(true);
echo(defaults=defaults(false, 33)); // 42

// Regular local variables should not survive to the next tail recursion call
function scope_leak(b=false) = b ? x : let(x=42) scope_leak(true);
echo(scope_leak=scope_leak()); // undef

// ...however, "config variables" should
function scope_leak_config(b=false) = b ? $x : let($x=33) let($x=42) scope_leak_config(true);
echo(scope_leak_config=scope_leak_config()); // 42