Boo

empty strings are considered true in a boolean context

Details

  • Type: Wish Wish
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 0.8.2
  • Fix Version/s: 0.9
  • Component/s: Compiler
  • Labels:
    None
  • Number of attachments :
    0

Description

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?

Activity

Hide
Rodrigo B. de Oliveira added a comment -

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

Show
Rodrigo B. de Oliveira added a comment - it makes sense to provide an extension implicit boolean conversion operator for string. the compiler looks for implicit conversions for conditional blocks.
Hide
Cedric Vivier added a comment -

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?

Show
Cedric Vivier added a comment - 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?
Hide
Cedric Vivier added a comment -

This must works for ducks of course:

foo as duck = string.Empty
print "oops" if foo
Show
Cedric Vivier added a comment - This must works for ducks of course:
foo as duck = string.Empty
print "oops" if foo
Hide
Cedric Vivier added a comment -

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"?)
Show
Cedric Vivier added a comment - 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"?)
Hide
Rodrigo B. de Oliveira added a comment -

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

Show
Rodrigo B. de Oliveira added a comment - Both 'or' and 'and' are short circuited operators that return the last evaluated operand. From that follows that ("x" and "y") should return "y".
Hide
Rodrigo B. de Oliveira added a comment -

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.

Show
Rodrigo B. de Oliveira added a comment - 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.
Hide
Cedric Vivier added a comment -

Fixed in rev. 3199

Show
Cedric Vivier added a comment - Fixed in rev. 3199

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: