Deferred Evaluation

The problem

You’re writing a book, and you want to start off with a table of contents that lists each chapter along with its page number. It looks something like this:

# Table of Contents

Chapter one: page $$chapterOnePageNumber{}
Chapter two: page $$chapterTwoPageNumber{}

But how do you calculate $$chapterTwoPageNumber{}? You don’t know what page chapter two is on until you get to it: as you add pages to chapter one, $$chapterTwoPageNumber{} will increase. We need to access $$chapterTwoPageNumber{} before it’s been calculated.

The solution: deferred evaluation

The answer is to use deferred evaulation:

This

$$name = {Alex}
{{{Hello, $$name{}}}}
$$name = {Chris}

Becomes

Hello, Chris

Anything wrapped in triple braces is deferred until the entire document has been processed. In essence, TextCarver saves anything between triple braces until after the entire document has been parsed, then goes through the entire document again to evaluate anything that was deferred. It’s slow, but super useful (if you aren’t using deferred evaluation, TextCarver will skip the deferred pass altogether).

Next: Security