[OpenSCAD] if statement ignores non boolean values

Brad Pitcher bradpitcher at gmail.com
Mon Sep 19 02:17:14 CEST 2011


I agree it should look at non-bools as well. I made a pull request for a
patch that allows other types to be used in boolean expressions:
https://github.com/openscad/openscad/pull/25
I hope I thought of everything, I based it off the logic that PHP uses (
http://php.net/manual/en/language.types.boolean.php):

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements

On Sat, Sep 17, 2011 at 10:21 AM, William Adams <william_a_adams at msn.com>wrote:

>  Yah, I guess a warning at least would be in line.  I assume the logic here
> is:
>   bool ? (value == true ? true : false) : false
>
> And the 'problem' is in the type coercion.
>
> bool ? boolvalue : false
>
> and you actually want
>
> bool ? boolvalue : (intype == numeric ? (value == 0 ? false : true): false)
>
> or something like that.
>
> Perhaps it's just a simple bug, rather than an intentional feature.  I've
> actually been bitten by this a few times, but largely just use boolean
> values, probably from my usage of C#, which does coercions, but gives you
> warnings if you want.
>
>
> -- William
> ===============================
> - Shaping clay is easier than digging it out of the ground.
>
>
> http://internationalwilliam.spaces.msn.com/
>
>
> > Date: Sat, 17 Sep 2011 17:06:57 +0100
>
> > From: nop.head at gmail.com
> > To: openscad at rocklinux.org
> > Subject: Re: [OpenSCAD] if statement ignores non boolean values
>
> >
> > If it only handles boolean values it should complain if it gets
> > something that is not one or it should convert it to a boolean like
> > any other language.
> >
> > On 17 September 2011 16:49, William Adams <william_a_adams at msn.com>
> wrote:
> > > Why would it complain?  I thought OpenScad has 'true' and 'false',
> actual
> > > boolean values.  In this context, 1 != true.
> > >
> > > so
> > > if (1)
> > >   echo(true);
> > > else
> > >   echo(false);
> > >
> > > will echo: false
> > >
> > > That seems like 'correct' behavior, if a bit annoying to classical 'C'
> > > programmers.
> > >
> > > -- William
> > >
> > > ===============================
> > > - Shaping clay is easier than digging it out of the ground.
> > >
> > >
> > > http://internationalwilliam.spaces.msn.com/
> > >
> > >
> > >> Date: Sat, 17 Sep 2011 16:06:35 +0100
> > >> From: nop.head at gmail.com
> > >> To: openscad at rocklinux.org
> > >> Subject: [OpenSCAD] if statement ignores non boolean values
> > >>
> > >> if(1) { ... } is not treated as true but does not complain either. A
> > >> bit confusing as I can't think of another language that would not do
> > >> one of the other.
> > >> _______________________________________________
> > >> OpenSCAD mailing list
> > >> OpenSCAD at rocklinux.org
> > >> http://rocklinux.net/mailman/listinfo/openscad
> > >
> > > _______________________________________________
> > > OpenSCAD mailing list
> > > OpenSCAD at rocklinux.org
> > > http://rocklinux.net/mailman/listinfo/openscad
> > >
> > >
> > _______________________________________________
> > OpenSCAD mailing list
> > OpenSCAD at rocklinux.org
> > http://rocklinux.net/mailman/listinfo/openscad
>
> _______________________________________________
> OpenSCAD mailing list
> OpenSCAD at rocklinux.org
> http://rocklinux.net/mailman/listinfo/openscad
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rocklinux.net/pipermail/openscad/attachments/20110919/5926a810/attachment.html


More information about the OpenSCAD mailing list