buzz-language / buzz

👨‍🚀 buzz, A small/lightweight statically typed scripting language
https://buzz-lang.dev
MIT License
1.2k stars 34 forks source link

Allow per resume/resolve error handling #59

Closed giann closed 11 months ago

giann commented 2 years ago

Right now if a fiber fails, we can only handle errors at the fiber creation site. Allow to handle error on any resume or resolve:

fun count(num n) > str > num? {
    for (num i = 0; i < n; i = i + 1) {
        if (i == 2) {
        throw "an error occured";
    }
        yield i;
    }

    return "Counting is done!";
}

fun main() > void {
    fib<str, num?> counter = &count(10);

    num sum = 0;
    while (!counter.over()) {
        sum = sum + (resume counter catch 0) ?? 0;
    }

    print(resolve counter catch "Ooops);
}

A fiber that failed on a resume is over. A subsequent resume or resolve should raise an error.