Version: v0.7.1 - Beta.  We welcome contributors & feedback.

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'
$colors.pop()        //= 'purple'


//--------------------------------------------------------
//                          MAPS
//--------------------------------------------------------

// Maps are key/value pairs.

// They are like associative arrays in PHP, but
// with JSON-like syntax.

$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']  //= '' (a safe falsey value)

// Use bracket notation to add a new key.
$user['job'] = 'Trainer'


//--------------------------------------------------------
//                       IF / ELSE
//--------------------------------------------------------

if condition1 {
    ...
} else if condition2 {
    ...
} else {
    ...
}

// Parens are not required 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 'fn'.
fn hello($name) {
    return 'Hello ' ~ $name ~ '!'
}

hello('Theresa')  //= 'Hello Theresa!'


// An optional argument with default value.
fn 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 'r'.
$regex = r'\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 fn 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()

See Also 

See Language Design Notes.