In the prior post (String Optimization. How does it work?) I described an optimization of how the Assignment statement calls the expression evaluator for string concatenation statements. There are certain limitations with this optimization. For example, if you use a Public variable and call a routine that can modify the variable, you might get unexpected results.
MyPublicVar = MyPublicVar + MyRoutine(MyPublicVar)
Another example is passing the variable by reference:
MyVar = MyVar + MyRoutine(@MyVar)
This kind code is very hard to read and does not seem deterministic. Also, in C++, you can write identical constructs, which have undefined meaning.
A legitimate case is recursion. Over the last 8 years, we’ve had very few reports of customers running into this problem.
Try running the code below, and you’ll get 2 different results.
The optimization modifies the compiled code to evaluate the addend:
x = x + <something recursive>
is modified to be
x = “” + <something recursive>
and then the result is evaluated.
The recursive call will see only the modified code, which doesn’t do the append of the addend.
There are several easy workarounds:
x = x + y
x = “” + x + <something recursive>
This has been fixed for the next release.
?”Now doing valid res”
PROCEDURE recur(nLev,fOpt) as String
cstr= cstr+TRANSFORM(nLev)+” “+recur(nLev-1,fOpt)