test if variable is defined

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

test if variable is defined

Johannes Reinhardt
Hi everybody,

Today I found a nice way to do test whether a variable is defined.
This can for example be used to create modules with a variable number of
arguments, or mightier default arguments. I did not find it on the web,
so I thought I would share it here. The key function is very short:


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


(of course this is not failsafe for the case when you need to pass the
value "undef", but this should be rare)

This allows to do things like


module cube_row(c1,c2,c3,c4){
  if(defined(c1)){
    cube(c1);
  }
  if(defined(c2)){
    translate([2*c1,0,0])
    cube(c2);
  }
  if(defined(c3)){
    translate([2*(c1+c2),0,0])
    cube(c3);
  }
  if(defined(c4)){
    translate([2*(c1+c2+c3),0,0])
    cube(c4);
  }
}

cube_row(1,2,3);


Greetings

Johannes



--
Johannes Reinhardt <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

nophead
Thanks for the tip. It might be just what I need to have defaults that can be overridden by command line parameters.

On 17 June 2012 22:42, Johannes Reinhardt <[hidden email]> wrote:
Hi everybody,

Today I found a nice way to do test whether a variable is defined.
This can for example be used to create modules with a variable number of
arguments, or mightier default arguments. I did not find it on the web,
so I thought I would share it here. The key function is very short:


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


(of course this is not failsafe for the case when you need to pass the
value "undef", but this should be rare)

This allows to do things like


module cube_row(c1,c2,c3,c4){
 if(defined(c1)){
   cube(c1);
 }
 if(defined(c2)){
   translate([2*c1,0,0])
   cube(c2);
 }
 if(defined(c3)){
   translate([2*(c1+c2),0,0])
   cube(c3);
 }
 if(defined(c4)){
   translate([2*(c1+c2+c3),0,0])
   cube(c4);
 }
}

cube_row(1,2,3);


Greetings

Johannes



--
Johannes Reinhardt <[hidden email]>
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

Giles Bathgate-2
In reply to this post by Johannes Reinhardt
On 17 June 2012 22:42, Johannes Reinhardt <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

nophead
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 <[hidden email]> wrote:
On 17 June 2012 22:42, Johannes Reinhardt <[hidden email]> 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
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

Giles Bathgate-2
On 18 June 2012 20:53, nop head <[hidden email]> wrote:
> It only seems to work properly with function arguments.

I can't seem to get it to work:

    function foo(a,b) = a==undef;
    echo(foo());

> With ordinary variables you get a warning which says "WARNING: Ignoring
> unknown variable 'fred'." so you don't gain anything testing it.

You would still have to declare the undefined variable:

  fred=undef;

Declaration is not the same as undefined...(think 'null' vs
'undeclared variable' in c)

> 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.

Me too, I think I got all this working in RapCAD, so one could use
that for reference maybe ;)

Regards

Giles.

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

Johannes Reinhardt
In reply to this post by nophead
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 <[hidden email]> 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 <[hidden email]> wrote:
>
> > On 17 June 2012 22:42, Johannes Reinhardt <[hidden email]>
> > 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
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >


--
Johannes Reinhardt <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

nophead
But don't you get a warning every time the module is called when COMMANDLINE_A is not defined.

On 18 June 2012 21:24, Johannes Reinhardt <[hidden email]> wrote:
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 <[hidden email]> 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 <[hidden email]> wrote:
>
> > On 17 June 2012 22:42, Johannes Reinhardt <[hidden email]>
> > 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
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >


--
Johannes Reinhardt <[hidden email]>
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

Johannes Reinhardt
In reply to this post by Johannes Reinhardt
On Mon, 18 Jun 2012 22:24:58 +0200
Johannes Reinhardt <[hidden email]> wrote:

Or the same, more convenient wrapped up as a function:

function defined(a) = str(a) != "undef";
function cl_default(arg,cl_default,fallback) = defined(arg) ? arg :
defined(cl_default) ? cl_default : fallback;

module default_test(a_){
        a = cl_default(a_,COMMANDLINE_A,"fallback");
        echo(a);
}

default_test();

Greetings

Johannes

> 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 <[hidden email]> 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 <[hidden email]> wrote:
> >
> > > On 17 June 2012 22:42, Johannes Reinhardt <[hidden email]>
> > > 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
> > > [hidden email]
> > > http://rocklinux.net/mailman/listinfo/openscad
> > >
>
>
> --
> Johannes Reinhardt <[hidden email]>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad


--
Johannes Reinhardt <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

Johannes Reinhardt
In reply to this post by nophead

Yes I do, but a has the correct values. I assume that OpenSCAD inspects
both alternatives of the ternary operator, and therefore warns about
COMMANDLINE_A, even if it is not used.


On Mon, 18 Jun 2012 21:42:22 +0100
nop head <[hidden email]> wrote:

> But don't you get a warning every time the module is called
> when COMMANDLINE_A is not defined.
>
> On 18 June 2012 21:24, Johannes Reinhardt <[hidden email]>wrote:
>
> > 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 <[hidden email]> 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 <[hidden email]> wrote:
> > >
> > > > On 17 June 2012 22:42, Johannes Reinhardt <
> > [hidden email]>
> > > > 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
> > > > [hidden email]
> > > > http://rocklinux.net/mailman/listinfo/openscad
> > > >
> >
> >
> > --
> > Johannes Reinhardt <[hidden email]>
> > _______________________________________________
> > OpenSCAD mailing list
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >


--
Johannes Reinhardt <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

nophead
It is simply because when you call defined(COMMANDLINE_A) you are using the undefined variable  COMMANDLINE_A.

On 18 June 2012 21:49, Johannes Reinhardt <[hidden email]> wrote:

Yes I do, but a has the correct values. I assume that OpenSCAD inspects
both alternatives of the ternary operator, and therefore warns about
COMMANDLINE_A, even if it is not used.


On Mon, 18 Jun 2012 21:42:22 +0100
nop head <[hidden email]> wrote:
> But don't you get a warning every time the module is called
> when COMMANDLINE_A is not defined.
>
> On 18 June 2012 21:24, Johannes Reinhardt <[hidden email]>wrote:
>
> > 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 <[hidden email]> 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 <[hidden email]> wrote:
> > >
> > > > On 17 June 2012 22:42, Johannes Reinhardt <
> > [hidden email]>
> > > > 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
> > > > [hidden email]
> > > > http://rocklinux.net/mailman/listinfo/openscad
> > > >
> >
> >
> > --
> > Johannes Reinhardt <[hidden email]>
> > _______________________________________________
> > OpenSCAD mailing list
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >


--
Johannes Reinhardt <[hidden email]>
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

Johannes Reinhardt
Another way would be to define a variable with the fallback default
value, use this as default and override it from the commandline when
needed. This is even nicer, codewise:

COMMANDLINE_A = "fallback";

module default_test(a=COMMANDLINE_A){
        echo(a);
}

To override use openscad -D COMMANDLINE_A='"commandline"'

Greetings


On Mon, 18 Jun 2012 21:54:55 +0100
nop head <[hidden email]> wrote:

> It is simply because when you call defined(COMMANDLINE_A) you are using the
> undefined variable  COMMANDLINE_A.
>
> On 18 June 2012 21:49, Johannes Reinhardt <[hidden email]>wrote:
>
> >
> > Yes I do, but a has the correct values. I assume that OpenSCAD inspects
> > both alternatives of the ternary operator, and therefore warns about
> > COMMANDLINE_A, even if it is not used.
> >
> >
> > On Mon, 18 Jun 2012 21:42:22 +0100
> > nop head <[hidden email]> wrote:
> > > But don't you get a warning every time the module is called
> > > when COMMANDLINE_A is not defined.
> > >
> > > On 18 June 2012 21:24, Johannes Reinhardt <[hidden email]
> > >wrote:
> > >
> > > > 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 <[hidden email]> 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 <[hidden email]>
> > wrote:
> > > > >
> > > > > > On 17 June 2012 22:42, Johannes Reinhardt <
> > > > [hidden email]>
> > > > > > 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
> > > > > > [hidden email]
> > > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > >
> > > >
> > > >
> > > > --
> > > > Johannes Reinhardt <[hidden email]>
> > > > _______________________________________________
> > > > OpenSCAD mailing list
> > > > [hidden email]
> > > > http://rocklinux.net/mailman/listinfo/openscad
> > > >
> >
> >
> > --
> > Johannes Reinhardt <[hidden email]>
> > _______________________________________________
> > OpenSCAD mailing list
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >


--
Johannes Reinhardt <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

Giles Bathgate-2
On 19 June 2012 08:09, Johannes Reinhardt <[hidden email]> wrote:
> Another way would be to define a variable with the fallback default
> value, use this as default and override it from the commandline when
> needed. This is even nicer, codewise:

I agree, but then you are going to have to make a function such as:

    function defined(a) = a!="fallback";

Which seems a bit wrong to me. I just vote for the bug in openscads
handling of undef to be fixed, then your "default value" would be
undef e.g.

    COMMANDLINE_A=undef;

    module default_test(a=COMMANDLINE_A){
           echo(a);
    }

The simple test case which I expect to pass:

    if(undef==undef) echo("Should work!");
    if(undef!=undef) echo("Broken!");

Regards

Giles.

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

nophead
In reply to this post by Johannes Reinhardt
That doesn't work for me. I think command line definitions only override top level variables. Those defined in modules used by the top level cannot be overridden.


On 19 June 2012 08:09, Johannes Reinhardt <[hidden email]> wrote:
Another way would be to define a variable with the fallback default
value, use this as default and override it from the commandline when
needed. This is even nicer, codewise:

COMMANDLINE_A = "fallback";

module default_test(a=COMMANDLINE_A){
       echo(a);
}

To override use openscad -D COMMANDLINE_A='"commandline"'

Greetings


On Mon, 18 Jun 2012 21:54:55 +0100
nop head <[hidden email]> wrote:

> It is simply because when you call defined(COMMANDLINE_A) you are using the
> undefined variable  COMMANDLINE_A.
>
> On 18 June 2012 21:49, Johannes Reinhardt <[hidden email]>wrote:
>
> >
> > Yes I do, but a has the correct values. I assume that OpenSCAD inspects
> > both alternatives of the ternary operator, and therefore warns about
> > COMMANDLINE_A, even if it is not used.
> >
> >
> > On Mon, 18 Jun 2012 21:42:22 +0100
> > nop head <[hidden email]> wrote:
> > > But don't you get a warning every time the module is called
> > > when COMMANDLINE_A is not defined.
> > >
> > > On 18 June 2012 21:24, Johannes Reinhardt <[hidden email]
> > >wrote:
> > >
> > > > 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 <[hidden email]> 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 <[hidden email]>
> > wrote:
> > > > >
> > > > > > On 17 June 2012 22:42, Johannes Reinhardt <
> > > > [hidden email]>
> > > > > > 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
> > > > > > [hidden email]
> > > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > >
> > > >
> > > >
> > > > --
> > > > Johannes Reinhardt <[hidden email]>
> > > > _______________________________________________
> > > > OpenSCAD mailing list
> > > > [hidden email]
> > > > http://rocklinux.net/mailman/listinfo/openscad
> > > >
> >
> >
> > --
> > Johannes Reinhardt <[hidden email]>
> > _______________________________________________
> > OpenSCAD mailing list
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >


--
Johannes Reinhardt <[hidden email]>
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: test if variable is defined

nophead
I should add that all I am trying to do is have some echo statements that make a bill of materials that are turned off by default but can be enabled from the command line. It doesn't seem to be possible unless everything is in one file.

On 19 June 2012 10:59, nop head <[hidden email]> wrote:
That doesn't work for me. I think command line definitions only override top level variables. Those defined in modules used by the top level cannot be overridden.


On 19 June 2012 08:09, Johannes Reinhardt <[hidden email]> wrote:
Another way would be to define a variable with the fallback default
value, use this as default and override it from the commandline when
needed. This is even nicer, codewise:

COMMANDLINE_A = "fallback";

module default_test(a=COMMANDLINE_A){
       echo(a);
}

To override use openscad -D COMMANDLINE_A='"commandline"'

Greetings


On Mon, 18 Jun 2012 21:54:55 +0100
nop head <[hidden email]> wrote:

> It is simply because when you call defined(COMMANDLINE_A) you are using the
> undefined variable  COMMANDLINE_A.
>
> On 18 June 2012 21:49, Johannes Reinhardt <[hidden email]>wrote:
>
> >
> > Yes I do, but a has the correct values. I assume that OpenSCAD inspects
> > both alternatives of the ternary operator, and therefore warns about
> > COMMANDLINE_A, even if it is not used.
> >
> >
> > On Mon, 18 Jun 2012 21:42:22 +0100
> > nop head <[hidden email]> wrote:
> > > But don't you get a warning every time the module is called
> > > when COMMANDLINE_A is not defined.
> > >
> > > On 18 June 2012 21:24, Johannes Reinhardt <[hidden email]
> > >wrote:
> > >
> > > > 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 <[hidden email]> 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 <[hidden email]>
> > wrote:
> > > > >
> > > > > > On 17 June 2012 22:42, Johannes Reinhardt <
> > > > [hidden email]>
> > > > > > 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
> > > > > > [hidden email]
> > > > > > http://rocklinux.net/mailman/listinfo/openscad
> > > > > >
> > > >
> > > >
> > > > --
> > > > Johannes Reinhardt <[hidden email]>
> > > > _______________________________________________
> > > > OpenSCAD mailing list
> > > > [hidden email]
> > > > http://rocklinux.net/mailman/listinfo/openscad
> > > >
> >
> >
> > --
> > Johannes Reinhardt <[hidden email]>
> > _______________________________________________
> > OpenSCAD mailing list
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >


--
Johannes Reinhardt <[hidden email]>
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad