Special Operators
Ternary ? :
The ternary operator is a shortcut for 'if a, then b, else c'.
It is used as an expression to determine a value.
$a = condition ? $result1 : $result2 // Same as... if condition { $a = $result1 } else { $a = $result2 } // Examples $a = true ? 'Yes' : 'No' //= 'Yes' $b = false ? 'Yes' : 'No' //= 'No' $badge = $isVerified ? '(Verified User)' : ''
Modulo %
Modulo gives you the remainder after dividing two numbers.
If the right side of the operator is negative, the result will also be negative.
10 % 2 //= 0 10 % 3 //= 1 10 % 4 //= 2 -10 % 3 //= 1 10 % -3 //= -1 // negative right side -10 % -3 //= -1 // negative right side
Odd/Even
Modulo is often used to check if a number is odd or even.
However, for clarity, you can simply call methods on the number itself.
$num = 20 $num.isEven() //= true $num.isOdd() //= false $num.isMultipleOf(5) //= true $num.isMultipleOf(7) //= false
Orval & Andval
These operators return the value of either side, depending on whether the left value is true or false.
Orval ||:
Orval (OR Value) can be used to assign a default value.
$id = $user.id ||: -1 // same as... if $user.id { $id = $user.id } else { $id = -1 }
Andval &&:
Andval (AND Value) can be used to check if an object is valid before accessing it further.
$id = $user &&: $user.id // same as... if $user { $id = $user.id } else { $id = $user // falsey value, like {} }
If-Assign :=
Because it’s a common mistake to accidentally use =
in place of ==
inside of a condition, THT triggers an error whenever this happens.
// ✕ Error - accidental assignment vs equality if ($a = 123) { ... } ^
However, there are cases when you want to keep the exact value of a condition, to use in the underlying block.
For this, you can use the If-Assign :=
operator.
$str = 'abc 123' if $num := $str.match(rx'\d+') { >> $num //= '123' }
Use parens (...)
to capture the exact value in compound expressions.
if ($len := $str.length()) > 3 { >> $len //= 7 }
For clarity, it must always create a new variable, not assign to an existing variable.
$num = 999 // ✕ Error - `$num` already exists if $num := $str.match(rx'\d+') { ... }
The variable is only visible in the scope of the underlying block.
if $len := $str.length() { ... } // ✕ Error - outside of block >> 'Length: ' ~ $len
Orset & Andset
Logic and assignment can be combined into a single operator.
Orset ||=
Orset (OR Set) can be used to assign a default value.
$numGuests ||= 1 // same as... if !$numGuests: $numGuests = 1
Andset &&=
Andset (AND Set) can be used to process a value further, if it exists.
$date &&= formatDate($date) // same as... if $date: $date = formatDate($date)
Cat ^^
The cat ^^
prefix operator applies advanced opti-meow-zation to a variable.
$moneyInBank = 301.67 print(^^$moneyInBank) //= 'MEOW'
Bitwise Operators
Because bitwise operations are rarely used in web development, THT uses a non-convential syntax (borrowed from Raku) to avoid accidental confusion with their Logical counterparts (e.g. &
vs. &&
).
+| bitwise OR +& bitwise AND +^ bitwise XOR +~ bitwise NOT +> bitwise SHIFT RIGHT +< bitwise SHIFT LEFT
These can be used with literal binary digits with the 0b
prefix.
Example:
$bin = 0b100 +& 0b110 //= 4 (0b100)