Skip to content

Docs/Language/Syntax & Keywords

Syntax & Keywords

The v1.0 keyword set: standard, universally understood keywords, with one keyword per concept.

~4 min read

On this page

Design

Fid uses keywords programmers already know, one keyword per concept, and no macros. Raw memory access and C calls must sit inside an explicit unsafe block, so they are easy to find.

Keywords

KeywordPurpose
fnFunction: named, anonymous, async, comptime
val / varImmutable / mutable binding
constTop-level compile-time constant
returnReturn (the last expression also returns)
typeType alias
capTrait (capability) declaration
use ... with { ... }Import with an explicit capability grant
comptimeCompile-time evaluation
deferCleanup at function exit
regionNamed region block
handle / do / with / resumeEffect handlers
spawn / async / await / selectConcurrency
sandbox / unsafe / txnCapability-free, unchecked, and transaction blocks
given / ensureContracts
extern / dyn / module / interruptFFI, trait objects, modules, interrupt handlers

Plus if/else, match, for/in, while, loop, break/continue, struct, enum, impl, pub, mut, as, true, false. Word keywords (state, model, given, …) may also name a parameter, field or local variable. A function can be declared with such a name, but a call to it does not parse, so do not name functions after keywords. Type names (i64, str, …) are never names. Semicolons after statements are optional.

Bindings

const MAX_SIZE: i64 = 1024;     // top-level constant

fn main() -> i64 {
    val x = 10;                 // i64 (the default integer type)
    val y: i32 = 20;            // explicit type
    var count = 0;
    count += 1;                 // var is mutable

    var z: i64;                 // no initializer
    if x > 5 { z = 1; } else { z = 2; }
    println(z);                 // OK: z is set on every path

    val (a, b) = (3, 4);        // tuple destructuring
    val n = 5;
    val n = "now a string";     // shadowing; the type can change
    println(n);
    println(x + count + a + b + MAX_SIZE);
    println(y);
    return 0;
}

Output: 1, now a string, 1042, 20.

Both of these are compile errors (each inside main):

val x = 1;
x = 2;        // error: cannot assign to 'x': it is declared with `val`.

var z: i64;
println(z);   // Init error: variable 'z' is used before being assigned a value.

Literals

fn main() -> i64 {
    println(42);          // integer (i64)
    println(3.14);        // f64
    println(0xFF);        // hex
    println(0b1010);      // binary
    println(1_000_000);   // separators
    println("hello");     // str
    println('c');         // character literal: its byte value
    val primes = [2, 3, 5];
    println(primes[1]);   // array literal, indexed
    return 0;
}

Output: 42, 3.14, 255, 10, 1000000, hello, 99, 3.

There are no literal suffixes (42i64 is a parse error), no byte strings (b"..."), and 1e5 is not a float: write 1.0e5. Raw strings are r"..." and r#"..."#.

Comments and names

// line, /* block */ (block comments do not nest); /// doc comments are read by scribe. Functions, variables and modules are snake_case; types and caps PascalCase; constants SCREAMING_SNAKE_CASE.

F-strings

Any expression can go inside {}:

struct Point { x: i64, y: i64 }

fn main() -> i64 {
    val name = "Ada";
    val x = 2;
    val y = 3;
    val p = Point { x: 1, y: 2 };
    var nums: Vec<i64> = Vec::new();
    nums.push(7);
    println(f"Hello, {name}!");          // Hello, Ada!
    println(f"{x} + {y} = {x + y}");     // 2 + 3 = 5
    println(f"Point: ({p.x}, {p.y})");   // Point: (1, 2)
    println(f"Length: {nums.len()}");    // Length: 1
    return 0;
}

There are no format specifiers: {x:.2} prints {x}. There is no escape for a literal brace.

Conditional compilation

@cfg(...) on a function or struct keeps it only when its condition holds:

@cfg(windows)
fn platform() -> str { return "windows"; }

@cfg(linux)
fn platform() -> str { return "linux"; }

fn main() -> i64 {
    println(platform());   // windows, on Windows
    return 0;
}

Conditions: windows, linux, macos, platform = "...", os = "...", arch = "x86_64" / target_arch = "x86_64", debug, release. The platform is the one fid runs on, not the --target; release always holds and debug never does. @cfg is not available on enums, impl blocks, globals or statements.

Report an issue