Borrow Checking in Nim 3.0
a day ago
- #borrow-checking
- #nim-lang
- #memory-safety
- Nimony provides memory safety through a simple borrow checking system combined with field-level ownership annotations.
- Key principles include safety by default for value types, simple teachable rules, field-level ownership clarity, graceful degradation when analysis is uncertain, and leveraging Nim's value semantics.
- Borrowing in Nimony involves establishing a witness path that must remain stable for the borrow's duration, with restrictions on certain operations during the borrow.
- Ownership annotations include `.unique` for exclusive ownership, `.cursor` for non-owning references, and regular `ref` fields for shared ownership.
- Borrow checking rules include prefix exclusion, read-only access to borrowed paths, disjoint siblings not conflicting, and nested borrowing working naturally.
- For complex paths or when mutating the parent during iteration, the `with` statement is used to safely move data temporarily.
- Nimony's borrow checking achieves memory safety through simple path-based rules, field-level ownership, and leveraging Nim's semantics, with escape hatches like `unchecked` and `with`.
- Performance benefits include acyclic types with no cycle collector overhead and zero-cost abstractions for safe iteration.