Variables

Variables are the heart of the TextCarver language.

This

$$name = {Alex}
Hello $$name{}

Becomes


Hello Alex

Variables always begin with a lowercase letter and built in commands always begin with an uppercase letter.

Early versus late evaluation

Code inside single braces is evaluated immediately:

This

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

Becomes



Hello Alex

Hello Alex

Code inside double braces is evaluated later, when it’s used:

This

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

Becomes



Hello Alex

Hello Chris

In the first example, the value of $$greeting is immediately set to Hello Alex. But in the second example, because it uses double braces, $$greeting is set to Hello $$name{}.

This is a somewhat subtle feature that’s easy to get wrong. As a general rule, use single braces when you’re referring to a variable in your text and double braces when you’re defining a big, complicated variable:

Hello, $$name{}
$$myBigComplicatedMacro = {{…

Multi-line variables

Variables can contain multiple lines:

This

$$people = {Alex
Blair
Chris}
$$people{}

Becomes

Alex
Blair
Chris

If you prefer, you can use a pipe symbol (|) to separate lines of a variable:

This

$$people = {Alex | Blair | Chris}
$$people{}

Becomes

Alex
Blair
Chris

Variables can have properties

You can assign properties to variables:

This

$$person.first = {Alex}
$$person.last = {Bobo}
My first name is: $$person.first{}
My last name is: $$person.last{}

Becomes

My first name is: Alex
My last name is: Bobo

You can assign multiple properties at once:

This

$$person = {|.first Alex |.last Bobo}
My first name is: $$person.first{}
My last name is: $$person.last{}

Becomes

My first name is: Alex
My last name is: Bobo

Indexing into variables

You can access individual lines of a variable:

This

$$people = {Alex
Blair
Chris}
$$people[1]{}

Becomes

Blair

Appending to variables

You can append to a variable using +=:

This

$$people = {Alex}
$$people += {Blair}
$$people{}

Becomes

Alex
Blair

Next: Conditionals