sketching new features
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 19 May 2023 20:27:12 +0000 (15:27 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 19 May 2023 20:27:12 +0000 (15:27 -0500)
tests/tagged_unions.onyx [new file with mode: 0644]

diff --git a/tests/tagged_unions.onyx b/tests/tagged_unions.onyx
new file mode 100644 (file)
index 0000000..c93eb89
--- /dev/null
@@ -0,0 +1,67 @@
+
+use core {*}
+
+/*
+Optional :: struct (T: type_expr) {
+    has_value: bool;
+    value: T;
+}
+*/
+
+Optional :: union (T: type_expr) {
+    None: void;
+    Some: T;
+}
+
+#inject Optional {
+    with :: macro (o: Optional($T), code: Code) {
+        switch o {
+            case .Some => v {
+                #unquote code(v);
+            }
+        }
+    }
+
+    or_default :: macro (o: Optional($T), default: T) -> T {
+        switch o {
+            case .Some => v do return v;
+            case .None      do return default;
+        }
+    }
+}
+
+// | tag type (Tag_Enum) | data... |
+
+
+Config :: struct {
+    name: Optional(str);
+}
+
+main :: () {
+    x := Optional(i32).{ Some = 123 };
+
+    c: Config;
+    c.name = .{ Some = "test" };
+
+    printf("{}.{}", typeof c.name.Some, c.name.Some);   // "Optional.tag_enum.Some"
+
+    x->with(#quote [v] {
+        printf("x has the value: {}\n", v);
+    });
+
+    y := x->or_default(0);
+    
+    switch x {
+        case .None {
+
+        }
+
+        case .Some => &value {
+            printf("x has the value: {}\n", value);
+        }
+    }
+}
+
+
+
+