summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md63
1 files changed, 61 insertions, 2 deletions
diff --git a/README.md b/README.md
index ea85869..b2c1e08 100644
--- a/README.md
+++ b/README.md
@@ -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).