[OpenSCAD] Openscad Indirect Functions

otto otto at 123phase.com
Mon Oct 17 03:23:33 EDT 2016

Python likes:

lambda x: x+2

For anonymous syntax.

Lisp likes

 (lambda (x) (+ arg 2))

For same operation.  I don't particularly like either construct but
either could be implemented.

I have now implemented 


in my prototype system.  It works just as well as the

  function @<tok_id>(<arglist>)=<expr>
I tried earlier. 

It is completely compatible with mixed name spaces and breaks no
earlier code.

If you wish to assign the function to a variable in the main name space
it can be done.  Dereferencing is done with the "@" symbol as well.

Any (almost) of the suggested syntaxes is easy to add to openScad.  The
(bison) parser works nearly flawlessly and was neatly programmed.  I
have had no trouble hacking the code.  

Here is an example of openScad code using the new syntax.

function reduce(func,vec) =
     len(vec)==2 ? @func(vec[0],vec[1]) : @func(vec[0],reduce(func,[for (i=[1:len(vec)-1]) vec[i]])); 

ECHO: 15
ECHO: 120
ECHO: -0.982406     

For me, Openscad1.1 is much more interesting than openScad 2.0..
That is, I do not believe that any changes should break previous code.
I have not yet posted the new code to github, but this time I will do so
as a complete clone of the current system in order to make it easier to

For me, to make my work possible, I wanted a system that implemented
some new functionality but in which I could have confidence that code
written by others continues to work.

I think the explicit dereferencing is better than a check during
compilation or execution and easier to implement and maintain.  I am
also considering the same kind of syntax for an anonymous module, where
an address can be passed around in a normal variable and dereferenced
when needed.  I considered @f() for functions and @m() for modules but
choose in my current prototype to use "@" unmodified.  Hacking the code
to support modules is more difficult and currently I have not started
trying, so I have no idea just how much I would be biting off.  Any
syntax for indirect functions is fairly easy for me at this point with
my current understanding of the code.

I have no intention of breaking any current code which means of course,
maintaining the separation of the name spaces.  When I get a clone of
the system set up for prototyping, I will add this kind of functionality
as I develop it and if people want it added to the base system they can
lobby for that or simply use the cloned/modified version.


On Sun, 16 Oct 2016 13:34:00 -0700 (MST)
Richard Urwin <soronlin+openscad at googlemail.com> wrote:

> Good point.
> What about ".", as in f.func? It's not as standard and it kind of
> shuts the door on aggregate types.
> Apostrophe?  f'func? It kind of disappears between those two f's.
> Tilde? f~func? Looks like a minus sign.
> Caret? f^func?
> The more I think about this, the more I feel it's all going to
> descend into Perl-style babble.
> How about: function func
> function and module are already recognised symbols and are not
> syntactically allowed in this context. It's more typing, but its
> meaning is absolutely clear.
> --
> View this message in context:
> http://forum.openscad.org/Convert-from-object-to-polygon-polyhedron-tp18522p18742.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
> _______________________________________________
> 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