Issue Details (XML | Word | Printable)

Key: BOO-1035
Type: Wish Wish
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Cedric Vivier
Reporter: Cedric Vivier
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Boo

empty strings are considered true in a boolean context

Created: 31/May/08 10:08 AM   Updated: 26/Jan/09 01:35 PM   Resolved: 26/Jan/09 01:35 PM
Return to search
Component/s: Compiler
Affects Version/s: 0.8.2
Fix Version/s: 0.9

Time Tracking:
Not Specified


 Description  « Hide
x = ""
if x:
    print "currently x is true in boolean context though it is empty"

Should not this condition return false ?
We could use string.IsEmptyOrNull when a string is used in a boolean context.

What do you think guys?



Cedric Vivier made changes - 31/May/08 10:09 AM
Field Original Value New Value
Description {code}
x = ""
if x:
    print "x is true in boolean context though it is empty"
{code}

Should not this condition return false ?
We could use string.IsEmptyOrNull when a string is used in a boolean context.


What do you think guys?
{code}
x = ""
if x:
    print "currently x is true in boolean context though it is empty"
{code}

Should not this condition return false ?
We could use string.IsEmptyOrNull when a string is used in a boolean context.


What do you think guys?
Rodrigo B. de Oliveira added a comment - 31/May/08 03:52 PM

it makes sense to provide an extension implicit boolean conversion operator for string. the compiler looks for implicit conversions for conditional blocks.


Cedric Vivier added a comment - 23/Jan/09 10:50 PM

Yup, makes sense but do you mean we should provide that extension as default in boo?

Also considering this is a light change for a very common case I think we should optimize it and wire it directly as we do for the other 'primitives' (to avoid a slight performance hit wrt extension method redirection and make generated assembly more friendly to static analysis [null reference tracking etc]).
Makes sense?


Cedric Vivier made changes - 23/Jan/09 10:50 PM
Status Open [ 1 ] In Progress [ 3 ]
Cedric Vivier added a comment - 24/Jan/09 07:45 AM

This must works for ducks of course:

foo as duck = string.Empty
print "oops" if foo

Cedric Vivier made changes - 24/Jan/09 08:02 AM
Fix Version/s 0.9 [ 13816 ]
Component/s Compiler [ 10940 ]
Cedric Vivier added a comment - 25/Jan/09 01:06 PM

Implementing this for conditional expressions (if/else) is quite simple, however I am not sure what to do with expressions such as:

x = "x"
y = "y"
print x or y #prints "x" (a string, not a boolean)

Currently this 'inline binary expression' causes a change of semantic when there is an implicit boolean conversion extension as in:

[extension]
def op_Implicit(s as string) as bool:
    return not string.IsNullOrEmpty(s)

x = "x"
y = "y"
print x or y #prints True (a boolean not a string)

but of course same problem would apply not only for string but for any type with an implicit boolean conversion.

Also, interestingly the following works though I guess it should not be allowed:

x = "x"
y = "y"
print x and y #prints "y" (why not "x"?)

Rodrigo B. de Oliveira added a comment - 25/Jan/09 07:28 PM

Both 'or' and 'and' are short circuited operators that return the last evaluated operand. From that follows that ("x" and "y") should return "y".


Rodrigo B. de Oliveira added a comment - 25/Jan/09 07:29 PM

op_Implicit should only be used to evaluate the operand trueness. The fact that's also being used as the resulting value is a bug.


Cedric Vivier added a comment - 26/Jan/09 01:35 PM

Fixed in rev. 3199


Cedric Vivier made changes - 26/Jan/09 01:35 PM
Resolution Fixed [ 1 ]
Status In Progress [ 3 ] Resolved [ 5 ]