if statement ignores non boolean values

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

if statement ignores non boolean values

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

Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

William Adams
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: [hidden email]
> To: [hidden email]
> 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
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

nophead
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 <[hidden email]> 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: [hidden email]
>> To: [hidden email]
>> 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
>> [hidden email]
>> http://rocklinux.net/mailman/listinfo/openscad
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
>
>

Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

Giles Bathgate-2

I agree it should either be a warning or cast 1 to true not just treat it as false along with 0

Regards

Giles

On Sep 17, 2011 5:07 PM, "nop head" <[hidden email]> wrote:
> 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 <[hidden email]> 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: [hidden email]
>>> To: [hidden email]
>>> 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
>>> [hidden email]
>>> http://rocklinux.net/mailman/listinfo/openscad
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> [hidden email]
>> http://rocklinux.net/mailman/listinfo/openscad
>>
>>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

William Adams
In reply to this post by nophead
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: [hidden email]
> To: [hidden email]
> 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 <[hidden email]> 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: [hidden email]
> >> To: [hidden email]
> >> 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
> >> [hidden email]
> >> http://rocklinux.net/mailman/listinfo/openscad
> >
> > _______________________________________________
> > OpenSCAD mailing list
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >
> >
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

Brad Pitcher
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 <[hidden email]> 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> 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 <[hidden email]> 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: [hidden email]
> >> To: [hidden email]
> >> 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
> >> [hidden email]
> >> http://rocklinux.net/mailman/listinfo/openscad
> >
> > _______________________________________________
> > OpenSCAD mailing list
> > [hidden email]
> > http://rocklinux.net/mailman/listinfo/openscad
> >
> >
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad


Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

kintel
Administrator
In reply to this post by nophead

On Sep 17, 2011, at 17:06 PM, nop head wrote:

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

This was implemented in commit 6659ad47b0b07b66b62ca79f3929d361733cc67a in the visitor branch.
From the release notes:

"if() and else() can now take any value type as parameter. false, 0, empty string and empty vector or illegal value type will evaluate as false, everything else as true."

My implementation corresponds to that of Brad, except mine considers any string with length > 0 as true.

I'm uncertain about whether the string "0" should be considered false or not. If we make it false, what about the string "false", or the array [0] (thinking about strings as arrays of characters)?

 -Marius


Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

Christian Siefkes
On 09/27/2011 07:59 PM, Marius Kintel wrote:
> This was implemented in commit 6659ad47b0b07b66b62ca79f3929d361733cc67a in the visitor branch.
>>From the release notes:
>
> "if() and else() can now take any value type as parameter. false, 0, empty string and empty vector or illegal value type will evaluate as false, everything else as true."
>
> My implementation corresponds to that of Brad, except mine considers any string with length > 0 as true.
>
> I'm uncertain about whether the string "0" should be considered false or not. If we make it false, what about the string "false", or the array [0] (thinking about strings as arrays of characters)?

Or what about the string "0.0", or " 0 ", or "00"? I think it makes more
sense to consider ALL non-empty strings as true (and only the empty string
as false), everything else opens a can of worms...

Best regards
        Christian

--
|------- Dr. Christian Siefkes ------- [hidden email] -------
| Homepage: http://www.siefkes.net/ | Blog: http://www.keimform.de/
|    Peer Production Everywhere:       http://peerconomy.org/wiki/
|---------------------------------- OpenPGP Key ID: 0x346452D8 --
Sent from my toaster.


signature.asc (262 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

Brad Pitcher
I agree, all non-empty strings should evaluate to true.


On Tue, Sep 27, 2011 at 11:13 AM, Christian Siefkes <[hidden email]> wrote:
On 09/27/2011 07:59 PM, Marius Kintel wrote:
> This was implemented in commit 6659ad47b0b07b66b62ca79f3929d361733cc67a in the visitor branch.
>>From the release notes:
>
> "if() and else() can now take any value type as parameter. false, 0, empty string and empty vector or illegal value type will evaluate as false, everything else as true."
>
> My implementation corresponds to that of Brad, except mine considers any string with length > 0 as true.
>
> I'm uncertain about whether the string "0" should be considered false or not. If we make it false, what about the string "false", or the array [0] (thinking about strings as arrays of characters)?

Or what about the string "0.0", or " 0 ", or "00"? I think it makes more
sense to consider ALL non-empty strings as true (and only the empty string
as false), everything else opens a can of worms...

Best regards
       Christian

--
|------- Dr. Christian Siefkes ------- [hidden email] -------
| Homepage: http://www.siefkes.net/ | Blog: http://www.keimform.de/
|    Peer Production Everywhere:       http://peerconomy.org/wiki/
|---------------------------------- OpenPGP Key ID: 0x346452D8 --
Sent from my toaster.


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad


Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

Whosawhatsis
The character "0" has an ASCII value of 48 (decimal), so it should
evaluate to true. Only the \0 character should evaluate to false.

On Tue, Sep 27, 2011 at 11:22 AM, Brad Pitcher <[hidden email]> wrote:

> I agree, all non-empty strings should evaluate to true.
>
>
> On Tue, Sep 27, 2011 at 11:13 AM, Christian Siefkes <[hidden email]>
> wrote:
>>
>> On 09/27/2011 07:59 PM, Marius Kintel wrote:
>> > This was implemented in commit 6659ad47b0b07b66b62ca79f3929d361733cc67a
>> > in the visitor branch.
>> >>From the release notes:
>> >
>> > "if() and else() can now take any value type as parameter. false, 0,
>> > empty string and empty vector or illegal value type will evaluate as false,
>> > everything else as true."
>> >
>> > My implementation corresponds to that of Brad, except mine considers any
>> > string with length > 0 as true.
>> >
>> > I'm uncertain about whether the string "0" should be considered false or
>> > not. If we make it false, what about the string "false", or the array [0]
>> > (thinking about strings as arrays of characters)?
>>
>> Or what about the string "0.0", or " 0 ", or "00"? I think it makes more
>> sense to consider ALL non-empty strings as true (and only the empty string
>> as false), everything else opens a can of worms...
>>
>> Best regards
>>        Christian
>>
>> --
>> |------- Dr. Christian Siefkes ------- [hidden email] -------
>> | Homepage: http://www.siefkes.net/ | Blog: http://www.keimform.de/
>> |    Peer Production Everywhere:       http://peerconomy.org/wiki/
>> |---------------------------------- OpenPGP Key ID: 0x346452D8 --
>> Sent from my toaster.
>>
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> [hidden email]
>> http://rocklinux.net/mailman/listinfo/openscad
>>
>
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
>
>

Reply | Threaded
Open this post in threaded view
|

Re: if statement ignores non boolean values

kintel
Administrator
In reply to this post by Brad Pitcher

On Sep 27, 2011, at 20:22 PM, Brad Pitcher wrote:

> I agree, all non-empty strings should evaluate to true.
>
Then it's settled :)

This is my test case FYI:
https://github.com/openscad/openscad/blob/visitor/testdata/scad/features/ifelse-tests.scad

 -Marius