Ownership Model
YaoXiang uses neither garbage collection (GC) nor lifetime annotations. Its memory safety is built on five concepts and one spectrum.
Five Concepts, One Spectrum
看一眼/原地改拿走共享持有复制一份系统级
│ │ │ │ │
&T Move ref concat() unsafe
&mut T 零拷贝编译器自动显式深拷贝 *T
零大小令牌默认选Rc/Arc 用户负责Move: Default Ownership Transfer
In YaoXiang, assignment = ownership transfer. This is the default behavior and involves zero copying:
Point: Type = { x: Float, y: Float }
main: () -> Void = {
p = Point(x=1.0, y=2.0)
p2 = p // Move! Ownership of p is transferred to p2
// p cannot be read afterward
// Want to modify p2? Rebind with mut
mut p3 = Point(x=3.0, y=4.0)
p3 = Point(x= p3.x + 1.0, y= p3.y + 1.0)
print(p2)
print(p3)
}Function parameters and return values are also moved:
Point: Type = { x: Float, y: Float }
// Parameter: passed by Move
process: (p: Point) -> Point = p
main: () -> Void = {
p = Point(x=1.0, y=2.0)
result = process(p) // p is moved away
print(result)
}&T / &mut T: Borrowing Tokens
If you do not want to take ownership and only want to temporarily "take a look" (&T) or "modify it in place" (&mut T), the compiler automatically generates zero-sized borrowing tokens:
use std.list
main: () -> Void = {
data = [1, 2, 3, 4, 5]
// The compiler automatically passes an &List(Int) token—it does not take ownership
print(list.len(data)) // 5
print(data) // ✅ data is still there; you only took a look
}&T and &mut T are zero-sized types—they exist at compile time and disappear at runtime. You do not need to write & manually; the compiler automatically chooses based on the usage context:
Point: Type = { x: Float, y: Float }
// Read-only access → automatically &T
// Note: do not name the function print, as that would shadow the built-in print function
show: (point: &Point) -> Void = {
print("({point.x}, {point.y})")
}
// Mutable modification → automatically &mut T
shift: (point: &mut Point, dx: Float, dy: Float) -> Void = {
point.x = point.x + dx
point.y = point.y + dy
}
main: () -> Void = {
mut p = Point(x=1.0, y=2.0)
show(p) // Passes &Point
shift(p, 1.0, 1.0) // Passes &mut Point
show(p)
}Key distinction: &T is copyable (shared and read-only), while &mut T is not copyable (exclusive and mutable). This is not a special rule—it follows from two type properties.
ref: Sharing Across Scopes
When you need to hold a value in multiple places at the same time, use ref:
main: () -> Void = {
data = [1, 2, 3, 4, 5]
// ref creates shared ownership
shared = ref data
// Cross-task sharing: the spawn block's receiver binding `(绑定) = spawn { 块 }` obtains the shared value,
// and the compiler automatically selects the reference counter:
// - Without crossing tasks → Rc (single-threaded reference counting)
// - Across tasks → Arc (atomic reference counting)
(shared) = spawn {
print(shared)
}
// You do not need to know the difference between Rc and Arc—the compiler chooses for you
}Copying: Explicitly Creating a New Value
When you need an independent copy, explicitly create a new value (YaoXiang does not perform implicit copying):
use std.list
main: () -> Void = {
original = [1, 2, 3]
// Note: lists have neither clone() nor copy() (both result in E1042 in practice).
// To create an independent copy, use concat to create a new list:
backup = list.concat(original, []) // Independent of original
// Each is independent: changing original does not affect backup
print(original) // [1, 2, 3]
print(backup) // [1, 2, 3]
}Explicit copying is explicit—you explicitly request a copy, unlike some languages that copy by default. ⚠️ In 0.8.2, std.list does not yet provide clone() / copy() (list.clone reports E1042); for now, the only way to make a copy is list.concat(xs, []).
No Lifetimes
YaoXiang has no lifetime 'a. This design choice comes from a key observation:
The borrowing conflict problem is essentially equivalent to verifying Hoare propositions. Leaving it to the type checker's proof pipeline to solve it in a unified way means that no additional borrow-checking framework is needed.
You do not need to annotate 'a or understand lifetimes—the compiler automatically verifies ownership safety during type checking.
No GC
The entire ownership model has no garbage collection. The timing of all memory releases is determined at compile time:
- After Move → the original variable is no longer usable, and RAII releases it automatically
- After ref → it is released when the reference count reaches zero
- When the scope ends → stack variables are released automatically
Zero GC pauses and zero runtime overhead.
Summary
| Operation | Keyword/Syntax | Copy? | When to use |
|---|---|---|---|
| Take ownership | Default behavior | Zero-copy | Function arguments, assignment |
| Take a look | Automatic &T | Zero-sized token | Read-only access |
| Modify in place | Automatic &mut T | Zero-sized token | Mutable modification |
| Shared ownership | ref | Reference counting | Across scopes/tasks |
| Explicit copy | No clone() (0.8.2 gap) | Create a new value | When an independent copy is needed |
| Raw pointer | unsafe + *T | Manual | System-level operations |
Remember: Move is the default, ref is for sharing, and copying must be explicit. Three rules, and no GC.
