DocsWelcome to Sable
About Sable
What Sable is for, the ideas it is built on, and what it deliberately leaves out.
Sable is a systems programming language for programs that want the expressiveness of C# and the ergonomics of a good standard library, but ship as a small native binary with predictable latency. It compiles ahead of time, through LLVM, to machine code. There is no garbage collector and no virtual machine: values are copied, objects are reference-counted, and the compiler checks how memory is used.
If you read C# or Swift, you can already read most Sable. Braces, fn, struct, class, protocol, extension, generics with where clauses and string interpolation all look the way you expect. What is new is underneath: Sable lets you say where memory lives, and checks that what refers to it never outlives it.
module Welcomeunion Shape { Circle(radius: F64) Rect(width: F64, height: F64)}fn area(_ shape: Shape) -> F64 { return match shape { case .Circle(let r) => 3.14159 * r * r case .Rect(let w, let h) => w * h }}fn main() { let shapes: Shape[] = [.Circle(radius: 1.0), .Rect(width: 2.0, height: 3.0)] for shape in shapes { print("area \(area(shape))") }}area 3.14159area 6.0What Sable is for#
Sable aims at a band of software that is badly served today: applications where a 200 ms garbage-collection pause, or a 150 MB runtime, rules a language out, but where writing everything in C or fighting a borrow checker costs too much. Games, creative tools, audio and simulation software, command-line tools, embedded Linux services, and servers that care about tail latency are all in that band.
It is just as clear about what it is not:
- Not a kernel or firmware language. Sable has a runtime. It is small, but it exists.
- Not a Rust replacement. Rust makes every value affine and annotates lifetimes, and buys strong guarantees with that. Sable trades some of those guarantees for code that reads like C# or Swift.
- Not a scripting language. There is no interpreter in the shipping story. The compiler does contain one, but it is used for compile-time evaluation and the REPL, not to run your program.
The six pillars#
Six design decisions shape the whole language. The rest of this book comes back to each of them.
Protocols, not inheritance#
Classes can form single-inheritance chains, but the intended unit of abstraction is the protocol: an interface with associated types, default implementations and generic constraints, which any type can be made to conform to after the fact with an extension. This is Swift's model, with C#'s explicit interface implementation for when two protocols' members collide.
Protocols are used two ways, and the syntax says which:
some Pis an opaque type: one concrete type, specialised at compile time, with no cost at run time.any Pis an existential: a box that can hold any conforming type, called through a table at run time.
Making the box visible where it is used is one of the most important decisions in the language. You always know when you are paying for dynamic dispatch. See Protocols.
Closed unions with exhaustive matching#
Sable keeps apart two features that other languages blur under the word "union":
- Nominal unions such as
union Shape { Circle(radius: F64), Rect(width: F64, height: F64) }are tagged sum types: closed, carrying payloads, and matched exhaustively. They are what Rust and Swift call anenumwith associated values. - Structural unions such as
Int | Stringare anonymous, in the TypeScript style, and usable in a signature without declaring anything. The compiler narrows them as you test them withis.
match is both a statement and an expression, with binding, tuple, record, range and array patterns and where guards. A match that misses a case is a compile error that names the missing cases. A plain enum is something else again: a named integer, or a set of flags. See Enums and Unions and Pattern Matching.
A tiered memory model, with reference counting by default#
Data in Sable lives in one of three tiers:
| Tier | Declared as | Cost |
|---|---|---|
| 0 | struct |
Inline and contiguous: no pointers, no reference count |
| 1 | region |
Bump-allocated, and freed all at once when the region's scope ends |
| 2 | class |
On the heap, reference-counted (ARC), with weak and unowned references |
A struct is the default for data, and a class is the default for things with identity: an object with a lifecycle, observers, or polymorphic behaviour. The optimiser moves values down the table when it can prove it is safe, so a class that never escapes a function may be allocated on the stack without a reference count. See Memory and Ownership.
Borrow checking that helps, without the wall#
Full Rust-style borrow checking is at odds with code that feels like C#. Sable keeps the cheap, useful part of it:
- Exclusive access. While a call changes a value through an
inoutparameter, nothing else may touch that value. This is Swift's law of exclusivity, checked at compile time where the compiler can prove it and at run time where it cannot. - Move-only types. A
@noncopy structcannot be copied, only moved, which suits files, sockets, locks and GPU buffers. Using a value after it has moved is a compile error. - References with inferred roots. A reference type,
T&, needs no lifetime annotations. The compiler works out what each reference is rooted in, and rejects any that could outlive it. Where Rust would need an explicit lifetime, Sable rejects the signature and suggests a fix.
The payoff is speed as well as safety: a function the compiler knows does not keep its arguments needs no reference-count traffic for them at all.
Inline layout, when you ask for it#
An @inline_layout struct is laid out as one contiguous block with no interior pointers. Links inside it are relative offsets rather than machine addresses, so the whole value can be written to disk or sent over a network as its bytes, and read back without parsing, after the bytes have been validated. The idea comes from the Goose language, and Sable offers it as an attribute rather than forcing it on every type. See Inline Layout.
Reflection you pay for only when you use it#
Every type has a little static metadata: its size, alignment, field offsets, name and conformances. Full reflection (listing members, calling them dynamically, reading attributes) is available only for types marked @reflectable, or reached from one. The build computes which types reflection actually needs, so serialisation, UI binding and plugins keep working while a hello-world program stays small. See Casting, Metatypes and Reflection.
Influences#
Sable borrows openly, and is just as open about what it leaves behind.
| From | Taken | Left behind |
|---|---|---|
| C# | The shape of the syntax, properties, async/await, query-style methods on collections, Roslyn's compiler architecture |
Garbage collection, IL, class-first design |
| TypeScript | Structural unions, type aliases, flow-sensitive narrowing | Structural typing everywhere, any as an escape hatch |
| Swift | Protocols with associated types, some and any, ARC, value semantics, extensions, argument labels, guard, typed throws |
Objective-C interop, source-breaking evolution |
| Rust | Move semantics for @noncopy types, exhaustive matching, errors as values, Cargo's manifest, lock file and workspaces, coherence rules |
Lifetime annotations, affine-by-default values |
| Goose | Inline recursive layout, relative references, variant-sized unions, root inference for references, zero-copy loading | The ban on the heap, C as the only back end |
| Go | Build speed as a requirement, one formatter with no options, the toolchain as one binary | Interfaces satisfied by accident, nil zero values |
What Sable leaves out#
Some things are missing on purpose, and will stay missing:
- No implicit numeric conversion. Not even a widening one: an
I32becomes anI64only when you writeas I64. See The Basics. - No null by default. A
Stringis always a string. Only an optional,String?, may holdnone, and the compiler narrows optionals as you test them. - No exceptions for control flow. Errors are typed (
throws(ParseError)), checked, and compiled into an ordinary return value rather than unwinding tables. A panic, for a broken invariant, is separate and cannot be caught. - No macros, ever. Compile-time functions and
comptimeblocks compute values,@derivewrites the standard conformances, and source generators (ordinary Sable functions that write new files as a package builds) cover the rest. Generators can add code but never rewrite it, so there is no quoting syntax, no hygiene problem, and every tool reads generated code like any other. See Derivation and Source Generators. - No subclassing across modules by default. A class must be declared
opento be subclassed outside its module, so the compiler is free to devirtualise calls.
Status#
Sable is pre-1.0. The compiler, runtime, standard library, package manager and language server all work end to end and are tested, but nothing is stable yet. Expect source changes between versions, and don't ship production code on it.
Where this book describes something that is not built yet, it says so. What is still missing is tracked in the language repository's GitHub issues.
How this book is organised#
The book follows the shape of The Swift Programming Language:
- Welcome to Sable: this chapter, Getting Started, which builds the toolchain and runs a first program, and A Sable Tour, which shows a little of everything.
- Language Guide: the language built up one idea at a time, starting with The Basics. Each chapter uses only what earlier chapters introduced.
- Language Reference: every part of the language stated completely and tersely, from Lexical Structure to the Summary of the Grammar.