Skip to content

[SUGGESTION] Value based exceptions (Zero-cost exceptions) #111

Closed
@redradist

Description

@redradist

This feature is based on https://youtu.be/ARYP83yNAWk?t=2227
Long time ago I wrote to you an email about it, but it was lost somewhere in history of civilisation ...
Anyway, I just want to extend your idea to use value based exceptions like it is done in Rust for example with Result<,> class and ? syntax
See the code:

run: (file: &std::string) -> int32_t throws Error1, Error2 {
    file := File::open(file).try;
    contents := std::string{};
    file.read_to_string(contents&).try;
    return contents.trim().parse().try;
}

This function could throw 2 exceptions Error1 or Error2.
Implementation could be done using union and value for describing type of error.
See godbolt possible underlying implementation: https://godbolt.org/z/a_vbNw

I suggest to convert all functions without throws keyword ( in cpp2 syntax) to function with noexcept (in C++ current syntax)

Also a list of exceptions could be generated automatically by analyzing which new cpp2 syntax functions is called from current context:

run: (file: &std::string) -> int32_t throws {
    file := File::open(file).try;
    contents := std::string{};
    file.read_to_string(contents&).try;
    return contents.trim().parse().try;
}

could be converted to:

run: (file: &std::string) -> int32_t throws Error1, Error2 {
    file := File::open(file).try;
    contents := std::string{};
    file.read_to_string(contents&).try;
    return contents.trim().parse().try;
}

Because cpp2 compiler knows that , for example, File::open(file).try and file.read_to_string(contents&).try could throw Error1 or Error2

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions