-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Imagine a stateful Hanna Code that maintains a count, and increases the count each time it's used:
[[count]]
[[count]]
[[count]]
You'd expect this output:
1
2
3
But you'll get this instead:
1
1
1
That's surprising, but it gets even more surprising:
Your Hanna Code will in fact be called three times, and the counter will in fact end up at "3"!
Here's what's happening.
All Hanna Codes are extracted up front, here. Then they're processed one by one, here. As each Hanna Code is processed, it gets replaced here. And that's the problem: the replacement is global; all matches get replaced, not just the first.
So in this example all three [[count]]s will be extracted, and the loop will iterate three times, but the first iteration will replace all occurrences of [[count]], leaving nothing for the next two iterations to replace.
(At first glance this looks like memoization; all occurrences end up with the first value that's computed for a given attribute list. I'm calling this "unintentional memoization" because typically you memoize to avoid work. In this case work isn't avoided, the result of the work simply isn't used.)