[OpenSCAD] Functions literals / higher order functions

Torsten Paul Torsten.Paul at gmx.de
Sun Nov 3 17:39:19 EST 2019


On 03.11.19 22:40, adrianv wrote:
> foo(function(x) sin(x))

Yes, all built-in functions and also those defined in
the previously existing function name() = expression
syntax have their own special namespace and are no values
that can be passed around. To do that they need to be
wrapped like that.

If a function literal is assigned to a variable, then
it can be passed around just like any other value too.

So:

f = function(x) x * x;

Is an unnamed function assigned to variable f, you
can assign it just like any other value to a different
variable:

g = f;

And because now g is of type function literal too:

echo(is_function(g)); // ECHO: true

It's possible to call it:

echo(g(5)); // ECHO: 25

Or pass it to a function or module:

module m(func) {
    echo(func(4));
}

m(g); // ECHO: 16

ciao,
  Torsten.



More information about the Discuss mailing list