Thank you to show another behavior aberration. No ONE would expect such unusual behavior easily fixed in the current broken expression evaluator.
When any part of the expression contains a variant, the compiler should switch to an expression evaluator with runtime type resolution. It’s a bit slower than a static one, but always will end with correct value.
So, for something like
Var r As Boolean
Var v As Variant = 0.5
r = v > 0 // MUST BE True or the evaluator is broken
Xojo currently sees something like this at compile time,
Boolean = ? > Integer
And lazy and wrongly it “solves it” statically at compile time:
Boolean = Integer(Variant) > Integer
→ r = Boolean(Integer(v) > 0)
Destroying the v value and ending with a wrong result.
You can only solve statically nodes with defined and known types at compile time, otherwise, runtime type resolution must be used during the evaluation to get the correct results.
This case should be:
Boolean = Variant(Double) > Integer // That double is enveloped at runtime and unknown at compile time
Once an expression contains a variant, it should evaluate it at runtime consulting the current types of both sides of the current expression node (x operator y) or sometimes just one and matching a static one as below
Boolean = Boolean(GreaterThan(Variant, Integer)) // This function unpacks the variant to the real value and performs the correct comparison or throws an error if invalid using whatever correct value it contains and returns 0 or 1 casting it back to Boolean for assignment
r = v > 0
--> r = _VariantGreaterThan(v, 0) // Yes, this will end as 0.5 > 0? -> True
How’s the correct behavior? This way:
What Xojo does? The unimaginable.
Var r1, r2 As Boolean
Var v As Variant = 0.5
Var d As Double = 0.5
r1 = v > 0 // MUST BE True, if False, the expression evaluator is broken, and it is
r2 = d > 0 // Same thing, just static, not enveloped at runtime
If not(r1 and r2) Then // Both logically MUST BE true
MessageBox "The expression evaluator is broken"
break
Else
MessageBox "The expression evaluator is OK"
End
Quit
If no one fix this bug, We need a compiler switch to declare as error any place containing any math/bitwise/boolean expression with a variant. This will enforce fixing program parts subject to silent errors, forcing the dev to replace such variant with a static one as Double, Currency, Integer…