Learn

A tour of the language

The syntax you will meet in the examples and in the compiler's own source, with just enough explanation to read it.

This is a reading guide, not a grammar. Every snippet is from, or reduced directly from, a program in the examples directory, so you can open the whole file. For why the resource rules look the way they do, read the ideas.

If you know C, Rust, or an ML-family language, most of this will look familiar. The unfamiliar parts are ! on linear types, & on lent parameters, and 'k brands. They're called out as they come up.

Functions and expressions

Functions use fn. A block's last expression is its value, and if and match yield values too. When inference has nothing else to go on, a numeric literal carries a suffix such as 0u32.

fn max_u32(a: u32, b: u32) -> u32 {
    if a > b { a } else { b }
}

This is from examples/basic. Struct values are written Point.{ x: a, y: b }, and an enum variant with a payload is built with .(, as in Option::Some.(value).

Packages and modules

A package has a kur.toml. Its .kur files are modules named by their paths, and lib.kur acts as a directory's module. Dependencies are explicit paths. The hello-world package looks like this (reformatted):

[package]
name = "example_hello"
source_root = "."
kind = "executable"
entry = "main"

[dependencies.std]
path = "../../lib/std"

Names come in with use, and a module can export a facade. In examples/modules the package's lib.kur exports a shapes namespace while the implementation stays in separate files. A type can be public without exposing its fields.

Linear types

A linear type's declaration and every use of its name end in !, as in Buffer!. A function that takes a linear parameter owns it unless it lends a place instead. To consume a value you pass it on, return it, destructure it, or call something that finishes its obligation. The compiler checks the same obligation across both branches of an if and around a loop.

examples/linear defines Handle! and shows valid control flow. examples/bad/ has programs that try to leak it or use it twice, along with the exact error each one earns.

Lending places

An & parameter receives the caller's place for the duration of a call. The counter example updates a caller's Counter through &counter and returns a value from it. Larger programs use a branded capability as the place parameter. The compiler lends &cx to every function working in one arena.

Ref[T, 'k] is a copyable description of storage in domain 'k, and the matching capability authorizes access. The graph example keeps cyclic nodes and edges as arena references, then switches from exclusive building to shared reading. The graph walkthrough goes through it step by step.

Generics and traits

forall binds type and brand parameters, and a where clause says what generic code needs from a type. Trait instances are resolved statically for each instantiation. The traits example defines Scale for two types and calls one generic function on both (reformatted here so the forall sits on its own line):

forall T where Scale[T].
fn scale_twice(x: T, factor: u32) -> T {
    x.scale(factor).scale(factor)
}

A bound such as Copy[T] lets the code use x more than once. Without it, T may be linear. Method lookup depends on the traits visible in the calling module, so you sometimes have to import a trait before a dot call works.

Errors and closures

Option and Result are ordinary enums used throughout the library. ? returns an error from any function whose result type can carry it. In examples/greet, main returns Result[(), Error] (trimmed):

fn main() -> Result[(), Error] {
    print("What's your name?\n")?;
    // `stdin` holds standard input until `finish` gives it back, with what
    // was read ahead past the line. The end of the input is an empty name.
    var input = stdin()?;
    let read = &input.read_line();
    match input.finish() {
        Ok(ahead) => ahead.destroy(),
        Err(e) => (),
    }
    let line = match read? {
        Some(line) => line,
        None => Buffer!::new(),
    };
    var name = capitalize(line);
    let printed = print(f"Hello, {&name}!\n");
    name.destroy();
    printed
}

Notice Buffer!, stdin() and {&name}: an owned buffer, the one reader of standard input, and a lend for formatting. The greeting walkthrough follows exactly this program.

Closures capture the values they mention. Capturing a linear value moves it into the closure, which then becomes linear itself. examples/thread_sum uses this to send a distinct owned buffer into each OS thread.

Unsafe and C interop

unsafe marks one operation: an unsafe or external call, a raw-pointer read or write, a cast to a pointer type such as x as *T, or anything else with an explicit obligation. Numeric casts are safe. It is not a mode for a whole block. A package can declare C sources and linker flags in kur.toml, and external symbols can be declared in kursal. examples/libuv_echo is a concrete C-library integration, and the standard library's contracts show how raw-pointer obligations are written down.

What's not there yet

There's no item-level privacy, so internal helpers appear in API docs. Trait methods can't have their own forall, and there are no superclass traits. Instances can't overlap: a generic instance such as forall T. instance Describe[T] rules out every more specific instance of that trait. Closures can't name a locally opened brand. Allocation failure aborts when a collection grows, though fallible constructors such as try_new (Owned!::try_new, for one) exist. The status list has the rest.

Once you can read the source, the compiler page shows what happens to it next.