Releases: sjorsdonkers/sum_type-jai
Releases · sjorsdonkers/sum_type-jai
v0.1.0
First release.
-
Polymorphism based
Sum_Type
struct -
Nested Sum_Types by defining a new struct with
using #as
and #run and#poke_name
.. -
sum_type(), set(), unwrap()
-
match
procedure taking procedures by variadic Any -
Run-time case procedure checks
-
Option type with some(), none() constructors and unwrap()
-
Result type with ok(), other() constructors, unwrap(), unwrap_other() and question mark proc q() to unwrap or return
Example showing a user-defined Sum_Type
:
main :: () {
MyFruit :: Sum_Type (
.{"Pineapple", u8},
.{"Mango", string},
.{"Kiwi", MyStruct},
);
fruitsicle := sum_type(MyFruit, .Pineapple, cast(u8)5);
set(*fruitsicle, .Mango, "Hello");
set(*fruitsicle, .Kiwi, MyStruct.{34, true});
match(fruitsicle,
(p: MyFruit.Pineapple) { print("Basic match Pineapple: %\n", p); },
(m: MyFruit.Mango) { print("Basic match Mango: %\n", m); },
(k: MyFruit.Kiwi) { print("Basic match Kiwi: %\n", k); },
);
}
MyStruct :: struct {
a: s32;
b: bool;
}
NU :: #import "Sum_Type";
#poke_name NU MyStruct;
#import "Sum_Type";
#import "Basic";
Example showing Option
:
opt := some(5);
if is_some(opt) {
print("Some: %\n", <<unwrap(opt));
} else {
print("None\n");
}
Example showing Result
:
level_2 :: (yes: bool) -> Result(s8, string) {
if yes {
return ok(cast(s8) 8, string); // Creates an Ok Result
}
return other(s8, "Oh nose!"); // Creates an Other Result
}
level_1 :: (yes: bool) -> Result(s8, string) {
val := q(level_2(yes)); // q() returns from this procedure or unwraps the result
return ok(val + 9, string);
}
out := level_1(false);
match(out,
(o: Result(s8, string).Ok) { print("Ok: %\n", o); },
(e: Result(s8, string).Other) { print("Other: %\n", e); },
);