fpco / inline-c

284 stars 50 forks source link

tryBlock catches C++ exceptions and returns Either #65

Closed abarbu closed 6 years ago

abarbu commented 6 years ago

The existing C++ exception binding mechanism 'catchBlock' is very inconvenient. Unlike 'block' it doesn't handle return values. It also always rethrows which is very non-idiomatic Haskell and not typesafe.

This introduces 'tryBlock'. It allows return values in almost every case (as long as the value can be constructed using {} or the return type is void). It returns either the exception or the return value which is more typesafe and is the more common error-handling mechanism everyone is used to.

abarbu commented 6 years ago

4 tests are already included in the PR. Two that test left / throwing (either always doing it or conditionally) and two that test right / not throwing (same). Was there anything else you were thinking of testing?

bitonic commented 6 years ago

4 tests are already included in the PR. Two that test left / throwing (either always doing it or conditionally) and two that test right / not throwing (same). Was there anything else you were thinking of testing?

not sure how i missed that, sorry. i've reorganized the code a bit and merged it, see https://github.com/fpco/inline-c/blob/80b816104f8853cbe655844891d24e63f06ab306/inline-c-cpp/src/Language/C/Inline/Cpp/Exceptions.hs . it's released as inline-c-cpp-0.2.2.0, thanks a lot for the contribution and sorry about the delay -- had a few busy weeks :|

abarbu commented 6 years ago

Awesome! Thanks for reorganizing and minting a new version.