Wednesday, March 10, 2010

Define overwrites

It is a little bit weird how X++ compiler handles the case when macros with the same name are defined in the class declaration of a class and in the one of its methods. The expected behavior for me is to either not allow to do that or to have a macro from class declaration to be applicable everywhere except for the method where it is redefined. But in AX the value defined in the method will overwrite the value defined in the class declaration and will be the same in all methods.
Example:
class TestDefine
{
    #define.A(‘A’)
}

public void method1()
{
    ;
    info(#A);
}

public void method2()
{
    #define.A(‘B’)
    ;
    info(#A);
}

If method1 or method2 will be called the ‘B’ will be received in the infolog and not ‘A’.

The next question will be what if define is overwritten in several methods. The answer is that the result is undetermined. It depends on X++ compiler internal flow and cannot be predicted. So, just avoid such constructions and even better avoid defines at all.

4 comments: