X-Sharp/XSharpPublic

Unhandled exception when using greater than or less than uType Logic

Closed this issue · 5 comments

There is a unhandled exception when using greater than or less than uType Logic.

An unhandled exception is not good, especially as a uType can be anything. If a comparison operator for a type is not implemented, it should always be false.

FUNCTION Start() AS VOID STRICT
	LOCAL uTest AS USUAL
	uTest := TRUE
	IF uTest > 0.0
		?"ok"
	ENDIF
	RETURN
//In this case the compiler said "Error XS0019	Operator '>' cannot be applied to operands of type 'logic' and 'real8'"
FUNCTION` Start() AS VOID STRICT
	LOCAL uTest AS LOGIC
	uTest := TRUE
	IF uTest > 0.0
		?"ok"
	ENDIF
	RETURN

We cannot produce a compiler error here like in the second example, because uType could be of the right type.
So the comparison is done at runtime.
There is not really an "unhandled exception" here.
Our runtime looks at the types of the 2 operands. In this case the first operand is a logic and the second a number.
As you can see in the code of the Greater Than operator:
https://github.com/X-Sharp/XSharpPublic/blob/main/src/Runtime/XSharp.RT/Types/Usual.prg#L665
this results in the runtime throwing a binary error with the message that the arguments are incompatible (there is no case for __UsualType.Logical)
I understand that you would have expected a return value of FALSE.
But that would hide logical errors in your code and is also not compatible to the behavior of VO and FoxPro

I haven't actually checked the compatibility with VO, if this is not guaranteed then that's done anyway, compatibility is more important.

The ticket can be closed.

VO throws the following exception for the first example:
image

When I extend the example to this:

FUNCTION Start() AS VOID 
   LOCAL uTest AS USUAL
   TRY
	uTest := TRUE
	IF uTest > 0.0
		?"ok"
   ENDIF
   CATCH e AS Exception
      ErrorDialog(e)
   END TRY
	RETURN 

Then X# produces the following error:
image

Those were my findings as well, indeed if we changed that, we would produce a VO incompatibility.