Language overview

The overall design of PickleScript: a C-family, declaration-oriented, garbage-collected language.

vlatest

PickleScript borrows its surface syntax from the C family: braces, parenthesised conditions, no semicolons. On top of that it adds the declaration, generics and pattern-matching features that a modern systems-adjacent language is expected to have.

module shapes

fn area(w: float, h: float) -> float {
    return w * h
}

fn main() {
    println("area = {area(3.0, 4.0)}")
}

The four rules that explain most of the language

1. A module is a list of declarations. Only declarations live at the top level. let x = 1 is not a module-level statement, it is a declaration; a bare expression like x + 1 cannot appear at the top level at all.

module valid

const LIMIT: int = 10

struct Point {
    var x: int
    var y: int
}

fn distance(p: Point) -> int {
    return p.x + p.y
}

test fn distance_adds_axes() {
    expect(distance(Point(1, 2))).toBe(3)
}

2. Declarations must appear before use. The resolver walks a module in order, so a function must be declared above its caller, a type above its use, and a test fn below the functions it exercises.

module ordered

fn main() {
    println(helper())     // error: `helper` is not declared yet
}

fn helper() -> int {
    return 1
}

3. Types are explicit. Function parameters and return types are written out rather than inferred from the first return statement.

4. let is immutable, var is mutable, const is a named constant. There is no way to rebind a let or assign to a const.

Binding forms

Form Meaning
let x: T = v Immutable binding
var x: T = v Mutable binding
const X: T = v Immutable named constant; by convention uppercase
fn main() {
    let name = "ada"          // cannot be reassigned
    var count = 0             // can be reassigned
    const LIMIT: int = 10

    count += 1                // fine
    // name = "grace"         // error: cannot assign to immutable binding
    // count = "ten"          // error: type mismatch
    println("{name} {count} {LIMIT}")
}

const works at module level and inside a function body, and is immutable in both positions.

Modules

Every file is a module. A module declaration names it, and import pulls in other modules. The name does not have to match the path on disk, a file declaring module anything.at.all compiles and runs exactly like one declaring module my.app.util.

module anything.at.all

fn main() {
    println("it compiles regardless of the module name")
}

See Modules and imports.

Runtime model

  • Values are garbage collected by default.
  • Strings are immutable byte sequences. There are no string methods; the operations live in std.text.
  • Lists and maps are built into the language rather than the standard library, and they have reference semantics.
  • Inside unsafe, raw pointers and manual allocation are available. See Manual memory.

Design decisions

A few things PickleScript deliberately does one way:

  • Expected absence is a value. Optionals (T? and none) carry "there is nothing here", and there is no Result type. Separately, try / catch / finally / throw exist for failures you do not want to push onto every caller. See Error handling.
  • Strings are immutable byte sequences. s[0] indexes a byte, and text operations live in std.text.
  • Absence is none. There is no nil, and no some(x) constructor. See Error handling.
  • int is 64 bits. There is no wider integer type.
  • if is a statement, and an expression in tail position. There is no cond ? a : b form.
  • Collection literals are non-empty. See Lists and maps.

Next steps