Page 1 of 1

if bool?

PostPosted: Mon Dec 21, 2009 11:23 pm
by Sopocles
Now, according to the "How-To,"
Code: Select all
if x
is equivalent to
Code: Select all
if x is not nil


but obviously,
Code: Select all
if false
does not execute the following statements, even though false is not nil.

I apologize if this has been addressed before, but the search function doesn't seem to have a "find this exact phrase" feature.

Some code that exhibits this:
Code: Select all
class NilTester
    def main is shared

        false_nillable as bool? = false

        for condition in [true, nil, false, false_nillable]

            print "Testing simple if statement with conditional [condition]:"

            if condition
                print "\tConditional [condition] PASSES the if statement."
            else
                print "\tConditional [condition] does NOT pass the if statement."

            print "Testing \"is not nil\" if statement with conditional [condition]:"

            if condition is not nil
                print "\tConditional [condition] PASSES the if statement."
                print "Conditional [condition] is of type [condition.getType].\n"
            else
                print "\tConditional [condition] does NOT pass the if statement.\n"


and it's results:

Code: Select all
Testing simple if statement with conditional true:
        Conditional true PASSES the if statement.
Testing "is not nil" if statement with conditional true:
        Conditional true PASSES the if statement.
Conditional true is of type System.Boolean.

Testing simple if statement with conditional nil:
        Conditional nil does NOT pass the if statement.
Testing "is not nil" if statement with conditional nil:
        Conditional nil does NOT pass the if statement.

Testing simple if statement with conditional false:
        Conditional false does NOT pass the if statement.
Testing "is not nil" if statement with conditional false:
        Conditional false PASSES the if statement.
Conditional false is of type System.Boolean.

Testing simple if statement with conditional false:
        Conditional false does NOT pass the if statement.
Testing "is not nil" if statement with conditional false:
        Conditional false PASSES the if statement.
Conditional false is of type System.Boolean.



cobra -version tells me that I'm running 0.8.0 post-release; I just got the latest version off the svn repository.

Re: if bool?

PostPosted: Tue Dec 22, 2009 5:52 am
by hopscc
What is truth asked Pontius Pilate^h^h^h^h^h^h^h^h^h^h^h Sophocles...
( Well it sure aint statistics said a voice from the crowd...)

This is alluded to in a number of places but only clearly articulated in the 'comparison to C#' on the wiki(under 'boolean conditions').

The howto comment is probably a bit misleading now since it refers to a change in behaviour done some time back..

non-nil is only one case of 'truth' :)

  • True is any of
    • non false (bool Type)
    • numeric not 0
    • non zero char
    • not nil
  • blank strings and empty collections are true (non nil) - must check .length for strings and .count for collections.

Thanks for highlighting this - I'll add a expansion/clarification to the wiki pages for statements that test truthfulness..