[OpenSCAD] Openscad Indirect Functions

otto otto at 123phase.com
Mon Oct 10 17:57:58 EDT 2016


That is very similar to what I have implemented and what I am
proposing.  I am not familiar with AngelScript.  It looks like in
AngelScript you declare that the argument "func" takes a function.  The
C/C++ like syntax of AngelScript doesn't really (IMHO) fit well with the
untyped and functional nature of OpenScad, but it is interesting to see
another language using @ as an indirection indicator, although it
appears (in AngelScript) in the argument list as a declaration of type
instead of in the body as a declaration of indirection at call time.

Is it possible for AngelScript to implement a function anonymously?
It doesn't look like it from the posted code.

Regards
Otto



On Mon, 10 Oct 2016 23:23:53 +0200
Carsten Arnholm <arnholm at arnholm.org> wrote:

> On 10. okt. 2016 19:13, otto wrote:
> > On Mon, 10 Oct 2016 10:44:13 +0100
> > nop head <nop.head at gmail.com> wrote:
> >
> >> So @xsq becomes both a name mangled function and a variable holding
> >> its name? That seems like a bit of a kludge to me.
> >
> > Well its like this, you can pass a function with the name of the
> > function in quotes.
> >
> > function div3(x)=x/3;
> > vec = [1,2,3];
> >
> > function map(func,vec) = [for (i=vec) @func(i)];
> > echo(map("div3",vec));
> >
> > And it works:
> >
> > Compiling design (CSG Tree generation)...
> > ECHO: [0.333333, 0.666667, 1]
> 
> 
> Sometimes I wonder why OpenSCAD reinvents standard language features
> of existing languages... below are some thoughts intended as
> inspiration. Your example code is applied to AngelScript, a language
> I use for 3d modelling:
> 
> funcdef double FUNC(double x);
> double div3(double x) { return x/3; }
> void map(FUNC @func, double[] @vec)
>     { for(uint i=0; i<vec.size(); i++) { vec[i]=func(vec[i]); }  }
> 
> void main()
> {
>     double[] vec = { 1,2,3 };
>     map(div3,vec);
>     for(uint i=0; i<vec.size(); i++) print(vec[i]+' ');
> }
> 
> 
> output is the expected:
>     0.333333 0.666667 1
> 
> First, a function signature 'FUNC' is defined, i.e. one taking and 
> returning a double. Second, an actual function 'div3' with a
> signature compatible with FUNC is defined. Third, the 'map' function
> where the 'func' parameter is passed 'by handle' using @ resembles
> your code (alternate syntax "FUNC@ func" also ok). The array 'vec'
> also passed 'by handle' so the caller sees the modified 'vec' after
> calling 'map'
> 
> If I had tried to call map using another function, e.g.
> double divn(double x, int n) { return x/n; }
> it would generate an error because 'divn' is incompatible with the
> FUNC signature. It is not clear from your example what happens if you
> did a similar mistake.
> 
> Just take this comment as food for thought & inspiration.
> 
> Regards
> Carsten Arnholm
> 
> 
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





More information about the Discuss mailing list