Variables

THP distinguishes between mutable and immutable variables.

Variables must be declared in THP to avoid issues with scoping and to know if they are mutable/immutable. It’s a compile error to use undeclared variables.

Variable names don’t start with a dollar sign $.

Variable names must begin with a lowercase letter or an underscore. Then they may contain lowercase/uppercase letters, numbers and underscores.

As a regex: [a-z_][a-zA-Z0-9_]*

Immutable variables

Defined with val, followed by a variable name and a value.

val surname = "Doe"
╰╴No statement matched
val year_of_birth = 1984
thp

It’s a compile error to attempt to modify it

val surname = "Doe"
╰╴No statement matched
surname = "Dane" // Error
thp

Datatype annotation

Written after the val keyword but before the variable name.

val String surname = "Doe"
╰╴No statement matched
val Int year_of_birth = 1984
thp

When annotating an immutable variable the val keyword is optional

// Equivalent to the previous code
 ╰╴No statement matched
String surname = "Doe" Int year_of_birth = 1984
thp

This means that if a variable only has a datatype, it is immutable.

It is a compile error to declare a variable of a datatype, but use another.

// Declare the variable as a String, but use a Float as its value
 ╰╴No statement matched
String capital = 123.456
thp

Mutable variables

Defined with var, followed by a variable name and a value.

var name = "John"
          ╰╴Invalid variable declaration
var age = 32 age = 33
thp

Datatype annotation

Written after the var keywords but before the variable name.

var String name = "John"
   ╰╴Invalid variable declaration
var Int age = 32
thp

When annotating a mutable variable the keyword var is still required.

// Equivalent to the previous code
var String name = "John"
    ╰╴Invalid variable declaration
var Int age = 32
thp