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.
let and const
Section titled “let and const”const is val, a name that can’t be given a new value. let is var. There are no semicolons.
const realm = "Lumbridge";let online = 0;online += 1;val realm = "Lumbridge"var online = 0online += 1Types go after the name, as in TypeScript. number splits into Int for whole numbers and Double for fractions, and 5 / 2 is 2.
const level: number = 42;const chance: number = 0.05;const name: string = "Hans";const members: boolean = true;val level: Int = 42val chance: Double = 0.05val name: String = "Hans"val members: Boolean = truenull and undefined
Section titled “null and undefined”There is only null, and a type holds it only when it ends in ?. ?. works as in TypeScript, and ?? is ?:.
let clan: string | undefined = undefined;const shout = clan?.toUpperCase() ?? "No clan";var clan: String? = nullval shout = clan?.uppercase() ?: "No clan"Functions
Section titled “Functions”function is fun, and the return type comes after the brackets.
function reward(level: number, bonus = 0): number { return level * 10 + bonus;}fun reward(level: Int, bonus: Int = 0): Int { return level * 10 + bonus}Arrow functions
Section titled “Arrow functions”An arrow function is a lambda in { }. A lambda with one parameter can call it it.
const double = (n: number) => n * 2;const cheap = [4, 50, 20].filter((p) => p < 30);val double = { n: Int -> n * 2 }val cheap = listOf(4, 50, 20).filter { it < 30 }Template strings
Section titled “Template strings”Every string is a template. ${} works as in TypeScript, and a plain name needs only $.
const item = "lobster";const price = 25;const line = `A ${item} costs ${price * 2} coins.`;val item = "lobster"val price = 25val line = "A $item costs ${price * 2} coins."Equality
Section titled “Equality”== compares by value: two strings, or two data classes, with the same contents are equal. === checks that both are the same object.
const a = "coins";const b = "coins";const same = a === b;val a = "coins"val b = "coins"val same = a == bswitch
Section titled “switch”switch is when, which gives back a value and never falls through.
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.";}val option = "Trade"val reply = when (option) { "Talk-to" -> "Hello." "Trade" -> "Take a look." else -> "No."}Interfaces and object shapes
Section titled “Interfaces and object shapes”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 :.
interface Tradeable { price: number }type Drop = { item: string; amount: number };const drop: Drop = { item: "coins", amount: 50 };const more = { ...drop, amount: 100 };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)Classes
Section titled “Classes”A class’s constructor parameters are in its header. val or var there makes them properties.
class Shop { constructor(public name: string, private stock: number) {} sell() { this.stock -= 1; }}const store = new Shop("General store", 40);class Shop(val name: String, private var stock: Int) { fun sell() { stock -= 1 }}
val store = Shop("General store", 40)Arrays
Section titled “Arrays”An array is a List, which can’t change, or a MutableList, which can. The functions you know are there.
const bag: string[] = ["bones"];bag.push("coins");const names = bag.map((i) => i.toUpperCase()).join(", ");val bag = mutableListOf("bones")bag.add("coins")val names = bag.map { it.uppercase() }.joinToString(", ")Maps and records
Section titled “Maps and records”A Map or a Record is a Map. to pairs a key with a value.
const prices: Record<string, number> = { bread: 4, cake: 50 };const cake = prices["cake"] ?? 0;val prices = mutableMapOf("bread" to 4, "cake" to 50)val cake = prices["cake"] ?: 0for...of is for (x in xs). A counting loop uses a range.
const party = ["Ana", "Bo"];for (const member of party) console.log(member);for (let i = 1; i <= 3; i++) console.log(`Wave ${i}`);val party = listOf("Ana", "Bo")for (member in party) Logger.info(member)for (i in 1..3) Logger.info("Wave $i")Event listeners
Section titled “Event listeners”A hook is the addEventListener of the game: the engine calls your block when the event happens.
window.addEventListener("load", () => console.log("Loaded"));on_login { player -> player.message("Welcome to the realm.")}Modules
Section titled “Modules”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.
export function depositFee(coins: number) { return Math.floor(coins / 100);}
// in another fileimport { depositFee } from "./bank/fees";public fun depositFee(coins: Int) = coins / 100import bank.depositFee
on_login { player -> player.message("Depositing 5,000 coins costs ${depositFee(5_000)}.")}