Question

The code here is X++. I know very little about it, though I am familiar with C#. MS says its similiar to C++ and C# in syntax.

Anyway, I assume the code below is a method. It has "Construct" as a keyword.

What is a construct/Constructor method? What does the construct keyword change when applied to the function? Also, am I wrong in assuming the code would create some sort of infinite loop?

My assumption is that its a method with a return type of "InventMovement".

static InventMovement construct(Common buffer, InventMovSubType subType = InventMovSubType::None, Common childBuffer = null)
{
    InventMovement movement = InventMovement::constructNoThrow(buffer,subType,childBuffer);

    if (!movement)
        throw error("@SYS20765");

    return movement;
}

Thanks! Kevin

Was it helpful?

Solution

Construct is not a keyword in X++, this is merely a static method called construct that returns an InventMovement class. It is used to allow you to create a derived class of a base class without having to know which derived class to create. This is how AX implements the Factory pattern. You will see this pattern used in AX in many places where there are abstract base classes.

InventMovement is an abstract base class for many other classes, such as InventMov_Purch and InventMov_Sales. You can't call new() on an abstract class, so instead of having a switch statement to call either new InventMov_Purch() or new InventMov_Sales() every time you need to create a InventMovement class, you use the InventMovement::construct() method to call the correct new() for you.

OTHER TIPS

There is no construct keyword in X++. The consturct idiom it's just how you implement the Factory design pattern in X++. Usually you'll find the 'construct' method in the base (abstract) class of the inheritance tree, and its purpose is to simply construct (instanciate and sometimes initialize) the correct subclass. After the correct subclass is created, the construct method returns a base class reference to the already created subclass. This is called polymorphism.

The construct idiom in X++ (rougly) translates to the following C++ pseudo-code:

class Base
{
public:
    static Base * construct(int aType);
    //some instance methods here
}; 

class Concrete1 : public Base
{
    // concrete implementation 1
};

class Concrete2 : public Base
{
    // concrete implementation 2
};

Base * Base::construct(int aType)
{
    switch(aType)
    {
        case 1:
            return  (Base*) new Concrete1();
        case 2:
            return (Base*) new Concreate2();
    }
}

DISCLAIMER: I know nothing about X++.

Based on the example there, it seems that the construct keyword is creating a constructor method.

In C++, you'd have

static Foo::Foo(int arg1) { this->val = arg1 }

My guess is that, for whatever reason, X++ requires the construct keyword on its constructor methods. It also appears that there's a constructNoThrow keyword, which presumably would be a constructor that's guaranteed not to throw an exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top