The Scarlet language
A statically-typed and expression-oriented programming language. Source files end in
.scrl.
import scarlet/array
type Shape {
Circle(r Float)
Rect(w Float, h Float)
}
fn area(s Shape) Float {
match s {
Circle(r) -> 3.14159 * r * r
Rect(w, h) -> w * h
}
}
pub fn main() {
shapes = [Circle(r: 2.0), Rect(w: 3.0, h: 4.0), Circle(r: 1.0)]
println('total area: ${array.fold(shapes, 0.0, fn(acc, s) acc + area(s))}')
}
Install
Install the binary. The script places it in ~/.scarlet/bin and adds that directory to your shell's path.
curl -fsSL scarlet.industries/install.sh | bash
Run
Write a file. Run it. A program starts at pub fn main(); module scope holds
declarations — imports, types, functions, constants — and no statements.
// Hello world, plus string interpolation with ${}.
pub fn main() {
println('hello, world')
name = 'Scarlet'
println('hello from ${name}')
println('2 + 2 = ${2 + 2}')
}
scarlet run hello.scrl
| Command | Function |
|---|---|
scarlet run <file> [args] |
Run a program. Trailing arguments reach the program through os.argv |
scarlet check <file> |
Type-check a program without running it |
scarlet fmt [path] |
Format source files in place. --check exits 1 on unformatted files |
scarlet repl |
Start an interactive REPL session |
scarlet lsp |
Start the language server |
scarlet dis <file> |
Print the compiled bytecode. --native prints the native code the JIT
produced beside it
|
scarlet man |
Emit the manual page to stdout |
scarlet upgrade [version] |
Replace the binary with a named version. The default channel is canary |
Programs compile to bytecode and run on a virtual machine with a JIT. A call in tail position reuses the current stack frame, so recursion runs flat, mutual recursion included.
Chapters
The reference is one page per topic. Read it in order, or go straight to the one you need.
-
Syntax
Bindings, blocks and functions. Almost everything is an expression, and nothing mutates.
-
Types
Enums, records, aliases and opaque types, and the exhaustive match that takes them apart.
-
Errors
Option and Result, the or fallback, and the <- chain the standard library is written in.
-
Collections
Persistent arrays and hash maps: an operation returns a new collection.
-
Numbers
Integers that wrap at 64 bits, IEEE-754 floats, bitwise operations, and exact money.
-
Binaries
Bit syntax: build a frame out of fields whose widths are measured in bits, and take one apart again.
-
JSON
A cursor over a tape, decoders that accumulate failures with their paths, and the three presence states.
-
Processes
Lightweight processes, typed mailboxes, and what a link, a kill and a monitor do when one ends.
-
Ports
Native code as a child operating-system process, read and written like a socket.
-
HTTP server
A handler is a plain fn(Request) Response; the library owns parsing, framing and keep-alive.
-
TLS client
An encrypted connection is a different type from a cleartext one, and certificate verification cannot be disabled.
-
Modules
Every file is a module. What pub exports, and how an import path resolves.
-
Standard library
The prelude that is always in scope, and what each standard library module carries.
-
Tooling
The formatter, the language server, and the VS Code and Zed extensions.