M2doc: why there are no variables, just constants?


the {m:let x=y} statement essentially defines a constant, because there is afaik no possibility to change the value of x once it has been set initially. This limitation has serious consequences, e.g. it is not possible to reinitialize a numbering of rows in generated tables if templates are used to generate them (recursively). Why are there no normal variables in m2doc? Or is there may be a workaround that would substitute them? br, M.

1 Like

The M2Doc language is based on the Acceleo language that is following the this specification from the OMG. It should be enough to generate anything. One thing that is missing is the ability to get the current index of a loop, which can be a limitation. You can write a state full service to keep track of the current loop index.

In your case you probably can pass the index to the recursive template.

template1(i : Integer)
template1(i + 1)

for instance. If it doesn’t help you can write a Java service.

Of course, the level (depth) of recursion (call nesting) can be represented by a template parameter, the value of which is incremented for each call. This is already done in our template. What has been meant is not this, but in general a normal, local or global variable the value of which can be changed. Only then the above described use case can be realised. Because without this (or at least a loop variable, as you have indicated) it is not possible to have a template which calls different other ‘leaf’ templates that are recursive and, at the same time, recognize when the ‘leaf’ template is called the first time within the top-level template. So if you have a template calling itself recursively and at the end of the call chain another ‘leaf’ template, you need a normal variable to flag, to signal to the ‘leaf’ template its initial invocation (as uaual, its value will be changed once the inital invocation is recognized). Is this explanation understandable?

Yes I understand. In this case you should write Java services that will increment a counter and get the current value of the counter.

Well, it is just that imho it is not justifiable if one has to resort to Java to implement a trivial counter or a variable value change. Rather it needs to be a standard part of m2doc available for everybody. That is why I have written this post in the first place.