Syntax Cheat Sheet
// This is a one-line comment
/*
This is a block comment
*/
//--------------------------------------------------------
// STRINGS
//--------------------------------------------------------
// Strings are created with a single-quote (')
'Hello World!'
// Strings are joined with the 'stringy' operator (~)
'Hello, ' ~ ' World!' //= 'Hello, World!'
// Backticks (`) are converted to single quotes
'That`s terrific!' //= 'That's terrific!'
// Multi-line strings are surrounded by quote fences
'''
This is
a multiline
string!
'''
//--------------------------------------------------------
// BOOLEANS / EQUALITY
//--------------------------------------------------------
// Booleans
true
false
// Negation
!true //= false
!false //= true
// Equality check with '=='.
// Acts like '===' in PHP (no type coercion).
1 == 1 //= true
1 == '1' //= false
// Inequality
1 != 1 //= false
1 != 2 //= true
// Comparison
1 < 2 //= true
1 > 2 //= false
2 >= 2 //= true
2 <= 2 //= true
//--------------------------------------------------------
// VARIABLES
//--------------------------------------------------------
// Initializing a variable
$letters = 'abcde'
$year = 2030
// Built-in types have methods via (.) 'dot'
'abdce'.length() //= 5
'abcde'.reverse() //= 'edcba'
// Short-hand math assignment
$year += 1 // same as 'year = year + 1'
$greeting = 'Hi'
$greeting ~= '!' // 'Hi!' (stringy assignment)
//--------------------------------------------------------
// LISTS
//--------------------------------------------------------
// Lists are an ordered sequence of values, of any type.
$colors = ['red', 'blue', 'yellow']
// Built-in methods
$colors.length() //= 3
$colors.join(', ') //= 'red, blue, yellow'
// Indexes start from one
$colors[1] //= 'red'
$colors[2] //= 'blue'
// Negative indexes count from the end
$colors[-1] //= 'yellow'
// Directly assign a value
$colors[3] = 'orange'
// Add & remove items
$colors.push('green') // Add to the end
$colors.pop() //= 'green'
// List Add operator: #=
$colors #= 'purple' // Same as 'push'
//--------------------------------------------------------
// MAPS
//--------------------------------------------------------
// Maps are key/value pairs.
// They are like associative arrays in PHP, but
// with JSON-like syntax.
$user = { name: 'Thomas', age: 25 }
// No multi-line commas
$user = {
name: 'Thomas'
age: 25
}
// Read field
$user['age'] //= 25
// Set field
$user['age'] = 33
// Or use dot (.) notation.
$user.age = 33
// Dot (.) notation is strict.
$user.job // ERROR. 'job' key doesn't exist.
// Bracket notation [] is not strict.
$user['job'] //= false (missing value)
// Use bracket notation to add a new key.
$user['job'] = 'Trainer'
//--------------------------------------------------------
// IF / ELSE
//--------------------------------------------------------
if condition1 {
...
} else if condition2 {
...
} else {
...
}
// Outer parens are not used in control flow statements.
if $a % 2 == 0 {
print('Number is even.')
}
// One-liner syntax using colon (:)
if $a % 2 == 0: print('Number is even.')
^
//--------------------------------------------------------
// LOOPS
//--------------------------------------------------------
// 'foreach' loops iterate through a list.
$colors = ['red', 'green', 'blue']
foreach $colors as $color {
print($color)
}
// Use the 'range' function to loop through numbers.
foreach range(1, 5) as $n {
print($n) //= 1 2 3 4 5
}
// Use slash (/) to get the key & value of a List or Map.
foreach $myMap as $key/$value {
print($key ~ ' = ' ~ $value)
}
// 'loop' repeats forever until you exit with 'break'.
// Use this instead of 'while'.
loop {
$color = Palette.getNextColor()
if !$color: break
print($color.name)
}
//--------------------------------------------------------
// FUNCTIONS
//--------------------------------------------------------
// Declare a function with 'fun'.
fun hello($name) {
return 'Hello ' ~ $name ~ '!'
}
hello('Theresa') //= 'Hello Theresa!'
// An optional argument with default value.
fun hello($name, $greeting = 'Hello') {
return $greeting ~ ' ' ~ $name ~ '!'
}
hello('Talako') //= 'Hello Talako!'
hello('Talako', 'Hi') //= 'Hi Talako!'
//--------------------------------------------------------
// SPECIAL STRINGS
//--------------------------------------------------------
// TypeStrings are secure literal strings that are
// prefixed with a type token.
// They take parameterized values via fill().
$query = sql'select * from user where id = {}'
$query.fill($userId)
$user = Db.select($query)
// Regular Expression strings are prefixed with 'rx'.
$regex = rx'\w+=(\d+)'
'foo=1234'.match($regex)[1] //= '1234'
//--------------------------------------------------------
// MODULES
//--------------------------------------------------------
// You can organize functions into separate module files.
// Each file has its own namespace.
// Prepend 'public' to functions that are exported.
// --- modules/ForumUser.tht ---
public fun hello($name) {
print('Hello ' ~ $name ~ '!')
}
// --- pages/home.tht ---
// Modules are automatically imported from the 'modules'
// folder when called.
ForumUser.hello('troll33') //= 'Hello, troll33!'
// Or import a module directly.
load('other/OtherModule')
OtherModule.otherFunction()
//--------------------------------------------------------
// SHORTCUTS
//--------------------------------------------------------
// THT avoids "magic" syntax, but it does provide
// a few 1-for-1 shortcuts.
>> $myData
// is the same as...
print($myData)
$colors = q[red blue green]
// is the same as...
$colors = ['red', 'blue', 'green']
$productName = .upperCase()
// is the same as...
$productName = $productName.upperCase()
$id = $user.id ||: -1
// is the same as...
$id = $user.id ? $user.id : -1
$numGuests ||= 1
// is the same as...
if !$numGuests: $numGuests = 1
$date &&= formatDate($date)
// is the same as...
if $date: $date = formatDate($date)
See Also
See Language Design Notes.