Replies: 1 comment
-
To me it seems that you use std::expected it is to get away from exceptions, why should it not be the same for python code. My suggestion would be that std::expected simply becomes a python class with the same functions as it has in c++ you can then write a handler in python to convert to exceptions if this is the route you want to go. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First of all, I'd like to say that pybind11 is an amazing library, thanks to everyone involved!
At my workplace, we use tl::expected for most of our error handling (and eventually will move to std::expected). Right now, we have to sort of "manually" unwrap these into Python on the bindings side. We often end up with large swaths of code that look something like the following:
It would be convenient if pybind11 provided a native unwrapper for these. I've made a prototype here, but it's not quite PR ready, there are still some questions I have about implementation:
std::expected
.a. Have a default (maybe std::runtime_error)
b. Allow users to specialize what errors map to what exceptions, perhaps just add a template parameter.
c. Allow users to provide custom conversion, for example if the error wraps multiple different kinds of errors
Another idea is to just require that users register the return type as an exception type known to pybind11, (and maybe also explicitly allow std::string).
PYBIND11_TYPE_CASTER
macro requires a name - for now I'm just calling it "expected", but I'm not sold. First, I'm not even sure if it needs a name if it's a one-way wrapper. Second, it would need to include the names of the value and error type surely... just not sure how.std::variant
usesUnion[...]
, but I don't think this is really like that.Would love to hear some thoughts on this, thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions