Skip to content

Hanna Codes appear to unintentionally "memoize" #28

@michaellenaghan

Description

@michaellenaghan

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions