Strange typing behaviour in the X# macro compiler

This forum is meant for questions and discussions about the X# language and tools
Post Reply
xor_ag
Posts: 6
Joined: Wed May 29, 2024 11:56 am
Location: Switzerland

Strange typing behaviour in the X# macro compiler

Post by xor_ag »

Hi all,

We have an application ported from VO that heavily relies on macro execution, and we discovered the following behaviour in the macro engine when evaluating expressions:

Code: Select all

local xMonth as usual
xMonth := MExec(MCompile("Month(Today())"))
// Result: xMonth contains Long (Int32)

xMonth := MExec(MCompile("Month(Today()) + 1"))
// Result: xMonth contains Int64, UsualType(xMonth) is 22!

// General examples
UsualType(MExec(MCompile("3 + 1")))    // Int32
UsualType(MExec(MCompile("3u + 1u")))  // still Int32
UsualType(MExec(MCompile("3u + 1")))   // Int64 !!
As it turns out, mixing a dword (unsigned int) with a signed integer will make the macro engine flip to Int64 for the entire expression. This gave us quite the headache as our application could not handle numeric usuals suddenly going Int64.

Is this a bug, or is it done on purpose to prevent overflows? Note that the X# compiler itself does NOT do this, it only happens when executing a macro. We are on X# version 2.21.
User avatar
Chris
Posts: 5471
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Strange typing behaviour in the X# macro compiler

Post by Chris »

Hi,

That's a complicated matter, in order to be compatible with VO, the compiler has different behavior depending on the dialect used, compiler options etc. The macro compiler is another complexity, and on top of that it's the c# backend which does its own thing as well.

I wouldn't say this particular case is done like that on purpose or it's a bug either, it's just a small inconsistent behavior that will be difficult to normalize.

Is it only the result of UsualType() that's causing you trouble? If yes, there's a way to redefine it yourself and always return INT even if the actual value is INT64, so that you don't need to change any other of your code. To do that, define this in a base library that is used by all apps and other libraries in your project, and the compiler will pick this one, instead of the one defined in the X# runtime:

Code: Select all

FUNCTION UsualType(uValue AS USUAL) AS DWORD
	LOCAL dwRet AS DWORD
	dwRet := Xsharp.RT.Functions.UsualType(uValue) // first call the original function from the X# runtime
	IF dwRet == INT64
		dwRet := INT
	END IF
RETURN dwRet
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
xor_ag
Posts: 6
Joined: Wed May 29, 2024 11:56 am
Location: Switzerland

Re: Strange typing behaviour in the X# macro compiler

Post by xor_ag »

Hi Chris,

thank you for your suggestion, that approach might be useful for us. In most cases, the exact numeric type doesn't matter, but we have some type checking code that gave us issues.
Post Reply