From e5d8c4219cfe5629a3ce4dbff01406a1817a788f Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Tue, 20 Jan 2026 12:06:28 +0000 Subject: Reference binding... --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'README.md') diff --git a/README.md b/README.md index cdba0f3..c09161d 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,21 @@ match shape { Rect(w, h) => print(f"Area: {w*h}"), Point => print("Point") } + +#### Reference Binding +To inspect a value without taking ownership (moving it), use the `ref` keyword in the pattern. This is essential for types that implement Move Semantics (e.g. `Option`, `Result`, non-Copy structs). + +```zc +var opt = Some(NonCopyVal{...}); +match opt { + Some(ref x) => { + // 'x' is a pointer to the value inside 'opt' + // 'opt' is NOT moved/consumed here + print(x.field); + }, + None => {} +} +``` ``` #### Loops @@ -418,7 +433,10 @@ autofree var types = malloc(1024); Zen C prevents double-free errors by enforcing **Move Semantics** for non-trivial types (structs) by default. - **Move by Default**: Assigning a struct variable transfers ownership. The original variable becomes invalid. -- **Copy Trait**: Implement the `Copy` trait to opt-out of move semantics for simple types (e.g. `Point`). +- **Management Strategies**: + - **`Copy` Trait**: Opt-out of move semantics for simple types. + - **`ref` Binding**: Use `match val { Some(ref x) => ... }` to inspect without moving. + - **Smart Derives**: `@derive(Eq)` uses references for comparison to avoid moves. ```zc struct Mover { val: int; } -- cgit v1.2.3