bool? can be counterintuitive
Posted: Fri Apr 11, 2008 1:40 pm
I don't think this one is a bug, but the behavior of nillable bools in implicit testing is something that could lead to the generation of very subtle bugs.
Not that nillable bools are used all that frequently, but when they are, it would probably be best to remind the programmer that it's not the boolean value that will be implicitly tested by generating an error or warning.
Applied ignorance combined with quick an dirty hacking led me to insert the following lines at line 2114 in Expr.cobra (in TestExpr._bindImp):
the results of which suggest that it might be a good candidate for such a verification to take place.
class Bob
shared
def eval(t as bool?) as String
return '[t] ==> ' + if(t, 'true', 'false')
def evalNot(t as bool?) as String
return 'not [t] ==> ' + if(not t, 'true', 'false')
def main
print Bob.eval(true) # true ==> true
print Bob.eval(false) # false ==> true
print Bob.eval(nil) # nil ==> false
print
print Bob.evalNot(true) # not true ==> false
print Bob.evalNot(false) # not false ==> false
print Bob.evalNot(nil) # not nil ==> true
Not that nillable bools are used all that frequently, but when they are, it would probably be best to remind the programmer that it's not the boolean value that will be implicitly tested by generating an error or warning.
Applied ignorance combined with quick an dirty hacking led me to insert the following lines at line 2114 in Expr.cobra (in TestExpr._bindImp):
_
if type.theWrappedType inherits DynamicType
_sharpTreatment = SharpTreatment.InvokeRuntime
+ else if type.theWrappedType is .compiler.boolType
+ trace type
+ _sharpTreatment = SharpTreatment.CompareToNull
else
_sharpTreatment = SharpTreatment.CompareToNull
the results of which suggest that it might be a good candidate for such a verification to take place.