Skip to content

Riftlang for JS/TS programmers

Riftlang reads much like TypeScript with types after names. The big differences: types are checked strictly, null is allowed only where a type says so, and a file’s folder is its module.

const is val, a name that can’t be given a new value. let is var. There are no semicolons.

TypeScript
const realm = "Lumbridge";
let online = 0;
online += 1;
Riftlang
val realm = "Lumbridge"
var online = 0
online += 1

Types go after the name, as in TypeScript. number splits into Int for whole numbers and Double for fractions, and 5 / 2 is 2.

TypeScript
const level: number = 42;
const chance: number = 0.05;
const name: string = "Hans";
const members: boolean = true;
Riftlang
val level: Int = 42
val chance: Double = 0.05
val name: String = "Hans"
val members: Boolean = true

There is only null, and a type holds it only when it ends in ?. ?. works as in TypeScript, and ?? is ?:.

TypeScript
let clan: string | undefined = undefined;
const shout = clan?.toUpperCase() ?? "No clan";
Riftlang
var clan: String? = null
val shout = clan?.uppercase() ?: "No clan"

function is fun, and the return type comes after the brackets.

TypeScript
function reward(level: number, bonus = 0): number {
return level * 10 + bonus;
}
Riftlang
fun reward(level: Int, bonus: Int = 0): Int {
return level * 10 + bonus
}

An arrow function is a lambda in { }. A lambda with one parameter can call it it.

TypeScript
const double = (n: number) => n * 2;
const cheap = [4, 50, 20].filter((p) => p < 30);
Riftlang
val double = { n: Int -> n * 2 }
val cheap = listOf(4, 50, 20).filter { it < 30 }

Every string is a template. ${} works as in TypeScript, and a plain name needs only $.

TypeScript
const item = "lobster";
const price = 25;
const line = `A ${item} costs ${price * 2} coins.`;
Riftlang
val item = "lobster"
val price = 25
val line = "A $item costs ${price * 2} coins."

== compares by value: two strings, or two data classes, with the same contents are equal. === checks that both are the same object.

TypeScript
const a = "coins";
const b = "coins";
const same = a === b;
Riftlang
val a = "coins"
val b = "coins"
val same = a == b

switch is when, which gives back a value and never falls through.

TypeScript
const option: string = "Trade";
let reply: string;
switch (option) {
case "Talk-to": reply = "Hello."; break;
case "Trade": reply = "Take a look."; break;
default: reply = "No.";
}
Riftlang
val option = "Trade"
val reply = when (option) {
"Talk-to" -> "Hello."
"Trade" -> "Take a look."
else -> "No."
}

An object shape becomes a data class, which you make by calling it. An interface stays an interface, and a class says it implements one with :.

TypeScript
interface Tradeable { price: number }
type Drop = { item: string; amount: number };
const drop: Drop = { item: "coins", amount: 50 };
const more = { ...drop, amount: 100 };
Riftlang
interface Tradeable {
val price: Int
}
data class Drop(val item: String, val amount: Int)
val drop = Drop(item = "coins", amount = 50)
val more = drop.copy(amount = 100)

A class’s constructor parameters are in its header. val or var there makes them properties.

TypeScript
class Shop {
constructor(public name: string, private stock: number) {}
sell() { this.stock -= 1; }
}
const store = new Shop("General store", 40);
Riftlang
class Shop(val name: String, private var stock: Int) {
fun sell() {
stock -= 1
}
}
val store = Shop("General store", 40)

An array is a List, which can’t change, or a MutableList, which can. The functions you know are there.

TypeScript
const bag: string[] = ["bones"];
bag.push("coins");
const names = bag.map((i) => i.toUpperCase()).join(", ");
Riftlang
val bag = mutableListOf("bones")
bag.add("coins")
val names = bag.map { it.uppercase() }.joinToString(", ")

A Map or a Record is a Map. to pairs a key with a value.

TypeScript
const prices: Record<string, number> = { bread: 4, cake: 50 };
const cake = prices["cake"] ?? 0;
Riftlang
val prices = mutableMapOf("bread" to 4, "cake" to 50)
val cake = prices["cake"] ?: 0

for...of is for (x in xs). A counting loop uses a range.

TypeScript
const party = ["Ana", "Bo"];
for (const member of party) console.log(member);
for (let i = 1; i <= 3; i++) console.log(`Wave ${i}`);
Riftlang
val party = listOf("Ana", "Bo")
for (member in party) Logger.info(member)
for (i in 1..3) Logger.info("Wave $i")

A hook is the addEventListener of the game: the engine calls your block when the event happens.

TypeScript
window.addEventListener("load", () => console.log("Loaded"));
Riftlang
on_login { player ->
player.message("Welcome to the realm.")
}

A file’s folder is its module, and public is the export: a public declaration can be imported by any file, by folder and name. A file with a public declaration is a library file. See Sharing code between files.

TypeScript
export function depositFee(coins: number) {
return Math.floor(coins / 100);
}
// in another file
import { depositFee } from "./bank/fees";
scripts/bank/fees.rift
public fun depositFee(coins: Int) = coins / 100
scripts/varrock/teller.rift
import bank.depositFee
on_login { player ->
player.message("Depositing 5,000 coins costs ${depositFee(5_000)}.")
}