Language tour

A quick pass over the core syntax. Each topic has a dedicated page with more detail; follow the links as you go.

Comments & statements

Single-line comments use // (there are no block comments). Statements are separated by newlines or semicolons; newlines inside (...) and [...] are ignored, so expressions can span lines.

// this is a comment

Variables

Declare with let; the type annotation is optional and inferred from the value:

let name = "alice"          // inferred string
let age: int = 30           // explicit annotation
let active: bool = true

Reassign with =, +=, or -=. Variables are block-scoped. See types & values for the full type list and number literals (0xFF, 0b1010, 1_000).

Functions

fn greet(name: string): string {
    return "Hello, " + name + "!"
}

let double = fn(x) { return x * 2 }   // anonymous function

Top-level functions are hoisted, support recursion and variadics (...args), and are first-class values. See functions.

Strings

Concatenate with +, or interpolate with backtick template strings:

let user = "Ada"
println("Hi, " + user)
println(`Hi, ${user} — ${3 + 4} new messages`)

Strings and arrays carry methods like .upper(), .split(), .map() — see value methods.

Operators

1 + 2 - 3 * 4 / 2 % 3      // arithmetic (+ also concatenates strings)
a == b   a != b   a < b    // comparison
a && b   a || b   !a       // logical, short-circuiting
a & b    a | b    a ^ b    // bitwise and / or / xor
a << n   a >> n   ~a       // shift left / right, bitwise not

Control flow

if / else if / else, while, and for..in (over arrays or range), with break and continue:

for i in range(10) {
    if i == 3 { continue }
    if i == 7 { break }
    println(i)
}

More in control flow.

Arrays & objects

let nums = [1, 2, 3]
nums = append(nums, 4)
let evens = nums.filter(fn(n) { return n % 2 == 0 })

let user = {name: "alice", age: 30}
println(user.name)

See arrays and classes & types.

Classes & errors

Langoost has classes with single inheritance, runtime-checked interfaces (is / as), and try / catch / throw / finally / defer for error handling:

class Dog extends Animal {
    fn speak() { return "woof" }
}

try {
    risky()
} catch err {
    println("failed: " + toString(err))
} finally {
    cleanup()
}

See classes & types and error handling.

Imports

import strings
import { abs, sqrt } from "math"

strings.upper("hi")     // "HI"
println(sqrt(16.0))     // 4

See modules & imports and the standard library — strings, http, json, crypto, sql, and more.

Built-in functions

Always in scope without an import: print, println, len, toString, toInt, toFloat, range, typeof, append, read, exit.