Ownership and Borrowing
Tyrus uses concise syntax and enforces ownership rules at compile time.
Move and Copy
Non-Copy values such as string move on assignment. The original binding becomes invalid after the move.
error
const greeting: string = "hello";
const copy: string = greeting;
print(copy); // hello
print(greeting); error
error[E0382]: use of moved value: `greeting`
at /src/main.tyr:4:7
print(greeting);
^^^^^^^^
at /src/main.tyr:2:22
const copy: string = greeting;
^^^^^^^^ value moved here; borrow `greeting` instead if it must remain usable
help: borrow `greeting` at the move site if it must remain usable later
help: for example: `const copy: &string = &greeting;`Primitive values such as i32, i64, f32, f64, and boolean are Copy and remain usable after assignment.
const score: i32 = 10;
const backup: i32 = score;
print(score); // 10
print(backup); // 10Function calls pass read-only arguments without consuming ordinary non-Copy caller bindings. Explicit references are available when an API needs to expose borrowing.
Borrowing
const original: string = "tyrus";
const borrowed: &string = &original;
print(borrowed); // "tyrus"
let counter: i32 = 0;
const mutable: &mut i32 = &mut counter;A borrow may take a part of a value: &basket.label borrows the field. The reference reads through basket, so basket is what stays borrowed, and assigning to it while the reference is live is refused. Borrowing two different fields at once is refused for the same reason, which is stricter than it has to be and never wrong.
interface Basket {
label: string;
items: i32[];
}
const describe = (value: &string): void => {
console.log(value);
};
const basket: Basket = { label: "fruit", items: [1, 2, 3] };
describe(&basket.label);
// fruitYou may have many immutable borrows or one mutable borrow, never both at the same time. Temporary borrows end at the end of the statement. A stored borrow ends at the last point its binding is mentioned, not at the closing brace: once nothing reads the reference again, the value it pointed at is free to be assigned, borrowed mutably, or moved, even though the binding is still in scope. A mention inside a loop body or captured by a closure counts, and keeps the borrow alive for as long as that construct can run. A mention inside a defer or errdefer body keeps it alive to the closing brace, wherever the finalizer is written, because that is where the finalizer runs. References cannot outlive the value they point to.
Inside an async function a borrow behaves the same way, with one added rule: it may not still be live at an await. Holding one across a suspension is rejected, "borrow b of a crosses await", which is also what makes borrows cheap there, since the state machine keeps the borrowed bytes in its frame and releases none of them.
Non-Lexical Lifetimes (NLL)
Tyrus implements Non-Lexical Lifetimes (NLL). Instead of keeping borrows alive until the closing curly brace of their lexical block (}), the borrow checker builds a Control Flow Graph (CFG) and uses backward dataflow liveness analysis to release loans immediately after their last actual use.
Mutable Borrow Followed by Mutation
A mutable borrow releases the original variable as soon as the reference is no longer read:
const main = (): void => {
let x: i32 = 5;
const r: &mut i32 = &mut x;
print(r); // last use of `r`
x += 1; // OK: `r` is dead, so `x` can be mutated
};Without NLL, r would remain live until the enclosing scope closed, rejecting x += 1.
Borrows Inside Conditional Branches
Loans taken inside an if branch expire when that branch finishes or ceases to use them, freeing the outer value:
let x: i32 = 5;
if (cond) {
const r: &i32 = &x;
print(r); // `r` dies here
}
x += 1; // OK: no active borrow in outer scopeMove After Borrow
Once a borrowed reference is no longer accessed, the owned value can be moved or consumed:
const consume = (data: string[]): void => {};
const s: string[] = ["tyrus"];
const r: &string[] = &s;
print(r[0]); // last use of `r`
consume(s); // OK: borrow ended, `s` can be movedReassignment of References
Reassigning a mutable reference to a new target immediately releases the borrow on the previous target:
let a: i32 = 1;
let b: i32 = 2;
let r: &mut i32 = &mut a;
print(r);
r = &mut b; // borrow on `a` ends; borrow on `b` begins
a += 1; // OK: `a` is no longer borrowed
print(r);Escape Prevention
NLL only shortens lifetimes to where references are actually needed; it never allows a reference to outlive its owner. Returning or storing a reference to a local variable that leaves scope is strictly rejected:
error
const invalidEscape = (): &i32 => {
const x: i32 = 42;
return &x;
};error
error[E0500]: cannot return reference to local variable `x`
at /src/main.tyr:3:12
return &x;
^^
help: references cannot outlive the value they point to; return an owned value insteadNullable Values
tyrus has no undefined, and user code should not use null as a normal initializer. Nullable values use T?, optional fields, optional parameters, external boundaries, narrowing and ??.
interface Profile {
nickname?: string;
}
const profile: Profile = {};
const nickname: string? = profile.nickname;
const fallback: string = nickname ?? "anonymous"; // "anonymous"
if (nickname) {
const present: string = nickname;
print(present); // "nickname value"
}Forced unwrap (value!) is intentionally discouraged and diagnosed by language tooling; prefer explicit narrowing or fallback values.
Deferred Initialization
const score: i32 = 85;
let label: string;
if (score >= 90) {
label = "A";
} else {
label = "B";
}
print(label); // "B"The compiler runs definite-assignment analysis and rejects reading a let x: T; binding before every path assigns it. const bindings must be initialized where they are declared.
