zeek / spicy

C++ parser generator for dissecting protocols & files.
https://docs.zeek.org/projects/spicy
Other
243 stars 37 forks source link

Expose `result` and `spicy::Error` types to Spicy to facilitate error handling. #1757

Closed rsmmr closed 2 months ago

rsmmr commented 2 months ago

Add result and spicy::Error types to Spicy to facilitate error handling.

The result and error types were already implemented internally HILTI-side, but not yet available to Spicy users. This exposes them to Spicy as well. To avoid name clashes with existing code, we don't introduce error as a new type keyword, but instead make it available as a library type spicy::Error.

Typical usage is something like this:

function foo() : result<int64> {
     ...
    if ( everything_is_ok )
        return 42;
    else
        return error"Something went wrong.";
}

if ( local x = foo() )
    print "result: %d " % *x;
else
    print "error: %s " % x.error();

The documentation has more specifics.

rsmmr commented 2 months ago

One change from the earlier draft: We now make the error type available as spicy::Error, that makes it accessible, but still avoids a new error keyword.

rsmmr commented 2 months ago

Do we want to use the opportunity to deprecate error as an ID in user code?

I don't think I'd want that become a reserved word at all, error just seems too common/intuitive to use in a Spicy program.