[OpenSCAD] test if variable is defined

Johannes Reinhardt jreinhardt at ist-dein-freund.de
Mon Jun 18 22:24:58 CEST 2012


Is this what you want to do:

function defined(a) = str(a) != "undef";

module default_test(a_){
	a = defined(a_) ? a_ : defined(COMMANDLINE_A) ? COMMANDLINE_A :
"fallback_default"; echo(a);
}

Then if I call openscad without commandline arguments I get

default_test();              ECHO:"fallback_default"
default_test("supplied");    ECHO:"supplied"

And if I start openscad with openscad -D COMMANDLINE_A='"commandline"'

default_test();              ECHO:"commandline"
default_test("supplied");    ECHO:"supplied"

with OpenSCAD 2012.06.02 under Ubuntu 11.10.

Greetings

Johannes


On Mon, 18 Jun 2012 20:53:33 +0100
nop head <nop.head at gmail.com> wrote:

> It only seems to work properly with function arguments.
> 
> With ordinary variables you get a warning which says "WARNING: Ignoring
> unknown variable 'fred'." so you don't gain anything testing it.
> 
> I would love a way of defining a default value for a variable and being
> able to override it on the command line. The documentation implies it can
> be done but it doesn't work.
> 
> On 18 June 2012 08:28, Giles Bathgate <giles.bathgate at gmail.com> wrote:
> 
> > On 17 June 2012 22:42, Johannes Reinhardt <jreinhardt at ist-dein-freund.de>
> > wrote:
> >
> > > function defined(a) = str(a) != "undef";
> >
> > You should be able to simply write this as:
> >
> >    function defined(a) = a != undef;
> >
> > Unfortunately there is a bug in openscad so that this doesn't work
> > correctly! The following test highlights the problem:
> >
> >    if(undef==undef) echo("Should work!");
> >    if(undef!=undef) echo("Broken!");
> >
> > Regards
> >
> > Giles
> > _______________________________________________
> > OpenSCAD mailing list
> > OpenSCAD at rocklinux.org
> > http://rocklinux.net/mailman/listinfo/openscad
> >


-- 
Johannes Reinhardt <jreinhardt at ist-dein-freund.de>


More information about the OpenSCAD mailing list