mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 20:11:17 +00:00
41 lines
644 B
Plaintext
41 lines
644 B
Plaintext
module test;
|
|
import libc;
|
|
|
|
faultdef NOPE;
|
|
|
|
fn int? eventually_succeed()
|
|
{
|
|
static int i = 0;
|
|
if (i++ < 3) return NOPE~;
|
|
return i * 3;
|
|
}
|
|
|
|
macro @retry(#function, int retries = 3)
|
|
{
|
|
var $Type = $typeof(#function);
|
|
fault e;
|
|
do
|
|
{
|
|
$Type? result = #function;
|
|
if (catch err = result)
|
|
{
|
|
e = err;
|
|
continue;
|
|
}
|
|
return result;
|
|
} while (retries-- > 0);
|
|
return e~;
|
|
}
|
|
|
|
fn void main()
|
|
{
|
|
int? result = @retry(eventually_succeed());
|
|
if (try result)
|
|
{
|
|
libc::printf("Got result: %d\n", result);
|
|
}
|
|
else
|
|
{
|
|
libc::printf("Failed :(\n");
|
|
}
|
|
} |