diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-19 12:53:47 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-19 12:53:47 +0000 |
| commit | 639c6ac65a1bd44b2ba0725fe7016a4920bf0950 (patch) | |
| tree | 47703f960633d3d4580022583134c28b96d5f36e /README.md | |
| parent | 526b7748cafcb5a00f8e30df88661f6059d79843 (diff) | |
Iterables and iterators :D
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 63 |
1 files changed, 61 insertions, 2 deletions
@@ -249,8 +249,8 @@ for i in 0..<10 { ... } // Exclusive (Explicit) for i in 0..=10 { ... } // Inclusive (0 to 10) for i in 0..10 step 2 { ... } -// Iterator/Collection -for item in vec { ... } +// Iterator (Vec, Array, or custom Iterable) +for item in collection { ... } // While while x < 10 { ... } @@ -422,6 +422,65 @@ var circle = Circle{}; var drawable: Drawable = &circle; ``` +#### Standard Traits +Zen C includes standard traits that integrate with language syntax. + +**Iterable** + +Implement `Iterable<T>` to enable `for-in` loops for your custom types. + +```zc +import "std/iter.zc" + +// Define an Iterator +struct MyIter { + curr: int; + stop: int; +} + +impl MyIter { + fn next(self) -> Option<int> { + if self.curr < self.stop { + self.curr += 1; + return Option<int>::Some(self.curr - 1); + } + return Option<int>::None(); + } +} + +// Implement Iterable +impl MyRange { + fn iterator(self) -> MyIter { + return MyIter{curr: self.start, stop: self.end}; + } +} + +// Use in Loop +for i in my_range { + println "{i}"; +} +``` + +**Drop** + +Implement `Drop` to define a destructor that runs when the object goes out of scope (RAII). + +```zc +import "std/mem.zc" + +struct Resource { + ptr: void*; +} + +impl Drop for Resource { + fn drop(self) { + if self.ptr != NULL { + free(self.ptr); + } + } +} +``` + #### Composition Use `use` to embed other structs. You can either mix them in (flatten fields) or name them (nest fields). |
