THT is in early Beta. We welcome contributors & feedback.

Language Tour - Intermediate

Summary 

These features are suitable for programmers that already have some programming experience.

They are not necessary in most THT programs.

Optional Parentheses 

Functions that take zero arguments can leave out the empty parentheses.

// with empty args
function doSomething() {
    // ...
}

// short form
function doSomething {
   // ...
}

// either way, functions are always called with parens
doSomething();

Keyword Shortcuts 

The following one-letter shortcuts can be used instead of the full keyword.

F = function
T = template

This helps you focus more on the “shape” of your code and the words that are unique to your program.

Example:

F addNumbers {
    return 1 + 2 + 3;
}

T resultHtml(name) {
    <h1>> Hello {{ name }}!
}

ImportantYour variable and function names should still be clear and descriptive! These are simply shortcuts for keywords that you will use thousands of times over a career.

Quoted (Quick) Lists 

A list prefixed with Q will automatically convert the contents to a list of strings. The tokens are separated by whitespace (spaces or newlines).

let colors = Q[red blue yellow green];
// Same as ['red', 'blue', 'yellow', 'green']

// or separated by newlines
let colors = Q[
    red
    blue
    yellow
    green
];

Operators 

Ternary ? :

The ternary operator is a shortcut for 'if a, then b, else c'

// If/else form
if (condition) {
    a = r1;
} else {
    a = r2;
}

// Ternary form
a = condition ? r1 : r2;

// Examples
let a = true ? 'Yes' : 'No';   //= 'Yes'
let b = false ? 'Yes' : 'No';  //= 'No'

OR Assign ||=, AND Assign &&=

Logic and assignment can be combined into a single operator.

// OR assign.  Good for assigning a default value.
numGuests ||= 1;
// same as...
if (!numGuests) { numGuests = 1; }

// AND assign.  Good for processing a value further.
joinDate &&= formatDate(joinDate);
// same as...
if (joinDate) { joinDate = formatDate(joinDate); }

OR Value ||:, AND Value &&:

Rather than return true/false, logical value operators return the value on the left or right.

// OR value.  Good for a assigning a default value.
let b = 0;
let a = b || 2;   //= true  (2 is a truthy, or non-zero, value)
let a = b ||: 2;  //= 2     (the truthy value itself)

// same as...
if (!b) { a = 2; }


// AND value
b = 1;
a = b && 2;   //= true  (1 is truthy)
a = b &&: 2;  //= 0     (the falsey value itself)

Regular Expressions 

Some string methods take a regular expression (aka Regex) string as an argument.

Regex strings are prefixed with an uppercase 'R'. (e.g. R'[a-z]+').

Compared to normal strings, backslashes are treated literally and spaces are ignored (the regex 'x' flag is on by default).

See Regular Expression Syntax for more.

text.match(R'\w+ \s+ \w*');

// case insensitive match
text.match(R'hello'.flags('i'));

Try/Catch 

When something goes wrong, it’s usually better to let the program die. A dead script can’t do any more damage, and is less likely to become a security vulnerability.

However, sometimes you want to provide a fallback for specific errors.

An Exception is an error that can be trapped with try/catch. The catch block will only run if there is an Exception in the try block.

let content = '';
try {
    // Will throw an Exception if it can't read the file.
    content = File.read('important.txt');
} catch (e) {
    // Error reading the file.  Try to restore it and continue.
    content = getDefaultContent();
    File.write('important.txt', content);
}

finally

A finally block can be used to define code that will run regardless of whether or not an Exception occurred.

This is optional, and is usually intended for cleanup logic that is invoked before the program ends.

try {
    // initial code
} catch (e) {
    // run if an Exception happens
    print(e.message());
} finally {
    // always run this after the above blocks complete
}

TipThis is usually unnecessary, thanks to PHP's share-nothing request cycle. Each THT request is automatically cleaned up after it ends (e.g. closed file handles and database connections).

die

You can trigger an Exception manually with die.

if (!fileExists) {
    die('Important file not found.');
}

To halt the program without triggering an Exception, use System.exit().