ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 492 forks source link

How can I fail a function without throwing exceptions? #1538

Open GasimGasimzada opened 8 months ago

GasimGasimzada commented 8 months ago

Assuming I have a function like this:

sol::state_view state(luaState);

state["test_fn"] = [](bool value) {
  if (!value) {
    // How can fail here without throwing exception?
  }
};

How can I fail this function?

Rochet2 commented 8 months ago

Hmm. I don't think sol has an error mechanism like that. And calling lua's error functions would not clean up any objects that sol has created I think as it would do longjmp.

What is the usecase? Why not use exception?

glebm commented 8 months ago

We have the same issue as our codebase has exceptions disabled. One convoluted workaround is to return a string with the error message and call error on the Lua side if the result is a string.

glebm commented 8 months ago

Ideally, sol would be integrated with std::expected (or tl::expected) so that the lamba could just return an expected<T, std::string> and sol would handle the unwrapping and calling Lua's error.