Files
c3c/resources/examples/retry.c3
2024-02-17 15:19:27 +01:00

44 lines
664 B
Plaintext

module test;
import libc;
fault TestErr
{
NOPE
}
fn int! eventually_succeed()
{
static int i = 0;
if (i++ < 3) return TestErr.NOPE?;
return i * 3;
}
macro @retry(#function, int retries = 3)
{
var $Type = $typeof(#function);
anyfault 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");
}
}