Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as(): make as() cast to use type_safety.enforce #1267

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

filipsajdak
Copy link
Contributor

The current implementation of as() for std::any, std::optional, and std::variant throws exceptions in case of tunability to cast. This behavior is inconsistent with runtime checked as() for signed/unsigned integral cast where it calls terminate().

This implementation switch runtime checked as() to use type_safety, which allows the setup of one approach for all type_safety issues.

The current behavior is to terminate, but it can be changed, e.g., to throw an exception. The following example is an excellent example of how it can be used.

void throw_error(CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM) {
    throw std::runtime_error(std::string("Type safety exception: ") + msg);
}

make_throwable: (inout cg : cpp2::contract_group) -> _ = {
    h := cg.get_handler();
    sh := :(pcg : *cpp2::contract_group) = {
        pcg*.set_handler(h$);
    };
    cg.set_handler(throw_error);
    return std::unique_ptr<cpp2::contract_group, decltype(sh)>(cg&, sh);
}

void expect_no_throw(auto&& fun) try {
    fun();
} catch(std::exception const& e) {
    std::cout << e.what() << std::endl;
} catch(...) {
    std::cout << "Unknown exception!" << std::endl;
}

main: () = {
    o : std::optional<int> = ();

    (_ := make_throwable(cpp2::type_safety)) {
        expect_no_throw( :() = {
            std::cout << (o$ as int) << std::endl; // that will throw
        });
    }

    std::cout << (o as int) << std::endl; // that will terminate
}

and the result is the following:

Type safety exception: 'as' cast failed for 'std::optional'
Type safety violation: 'as' cast failed for 'std::optional'
libc++abi: terminating

The current implementation for std::any, std::optional, and std::variant
throws exceptions. This behaviour is inconsistent to runtime checked
as() for signed/unsigned integral cast.

This implementation allows to setup one approch for type_safety issues.
Current behaviour is to terminate but it can be changed to e.g. throwing exception.
@hsutter
Copy link
Owner

hsutter commented Oct 31, 2024

Hi! Just checking in as I clean up the PRs with the relicensing (see #1322), is still intended to be still a draft PR? If so perhaps I'll close it for now and you can reopen this or a new PR once it's ready to look at?

As described in #1322, this didn't make release 0.8, so I will need a new one-time CLA that will cover all your future contributions. I've emailed you the new CLA, and one it's completed I can look at PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants