From 6f188cd25ac299d534dc692a52aee56d31d2440b Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 19 May 2023 15:27:12 -0500 Subject: [PATCH] sketching new features --- tests/tagged_unions.onyx | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/tagged_unions.onyx diff --git a/tests/tagged_unions.onyx b/tests/tagged_unions.onyx new file mode 100644 index 00000000..c93eb893 --- /dev/null +++ b/tests/tagged_unions.onyx @@ -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); + } + } +} + + + + -- 2.25.1