Add @try Builtin (#2333)

* Add @try macro
* Add @try_catch. Update release notes.

---------

Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
Zack Puhl
2025-07-28 18:50:56 -04:00
committed by GitHub
parent 2639338426
commit 9d2be2851b
3 changed files with 98 additions and 0 deletions

View File

@@ -28,6 +28,41 @@ fn void test_enum_by_name()
assert(@catch(enum_by_name(Tester, "GHI")) == NOT_FOUND);
}
faultdef SOME_FAULT, ABC_FAULT;
fn void test_try_catch()
{
int val;
int? x = ABC_FAULT?;
assert(@try_catch(val, x, ABC_FAULT)!!);
assert(val == 0);
assert(!@catch(@try_catch(val, x, ABC_FAULT)));
x = SOME_FAULT?;
assert(@catch(@try_catch(val, x, ABC_FAULT)) == SOME_FAULT);
x = 3;
assert(!@try_catch(val, x, ABC_FAULT)!!);
assert(val == 3);
}
fn void test_try_set()
{
assert(enum_by_name(Tester, "ABC")!! == Tester.ABC);
Tester val;
if (catch @try(val, enum_by_name(Tester, "ABC"))) abort("Test failure");
assert(val == Tester.ABC);
Tester another;
if (catch err = @try(another, enum_by_name(Tester, "GHI")))
{
assert(err == NOT_FOUND);
return;
}
abort("Test failure");
}
fn void test_likely()
{
int a = 2;