4 ms·
Functional exceptionless error-handling with optional and expected
- dep_b 9y agoIt almost makes me want to write some C++. Still an interesting observation that people want to add new stuff to an already humongous language like C++
- zrb 9y agoDoing the equivalent of "maybe chaining" isn't hard in c++ & quite elegant in c++17 in my view https://stackoverflow.com/questions/7690864/haskell-style-maybe-type-chaining-in-c11 https://stackoverflow.com/questions/7690864/haskell-style-ma...
- anderspitman 9y agoErnest question: If it supports the platform you're targeting and you're not missing any critical libraries, what other reasons are there not to use Rust over C++ for new projects? Assuming of course you're not already proficient in C++.
- sidlls 9y agoMaturity of the language, quantity of developers available, knowledge base ecosystem (this is more than just library availability, as it includes implementations to solve problems resulting from years of practice by thousands of engineers), interoperability with other languages' libraries, to name a few. In some cases Rust's "escape hatch" to get unsafe has more boilerplate than equivalent implementations in C++: when Rust's safety model isn't as important it might not make sense to use it. Personally, as a person very experienced with C++ I prefer Rust when it's available on the target platform. The low level, high efficiency computations I might do use numerical algorithms that neither Rust nor C++ are very good for, although C++ is slightly better in this case.
- _ZeD_ 9y agoJust thinking about the example... Let's say... just use excepions! This code std::optional<image_view> get_cute_cat (image_view img) { auto cropped = find_cat(img); if (!cropped) { return std::nullopt; } auto with_tie = add_bow_tie(*cropped); if (!with_tie) { return std::nullopt; } auto with_sparkles = make_eyes_sparkle(*with_tie); if (!with_sparkles) { return std::nullopt; } return add_rainbow(make_smaller(*with_sparkles)); } Becomes image_view get_cute_cat (image_view img) { auto cropped = find_cat(img); auto with_tie = add_bow_tie(*cropped); auto with_sparkles = make_eyes_sparkle(*with_tie); return add_rainbow(make_smaller(*with_sparkles)); } And it could throw a no_cat_found, a cannot_see_neck, or a cat_has_eyes_shut excepion
- HumanDrivenDev 9y agoIt's not a great usecase for optionals. a better one would be retrieving a value from a std::map - that one is pretty binary, it's either there or it isn't. To me optionals are for things where you'd consider it reasonable to return a null that represents "doesn't exist".
- Shoothe 9y agoBut if you already have null then the method could return null, empty optional or optional with value.
- HumanDrivenDev 9y agoI don't know about C++, but for example Java has a method "ofNullable" that will convert a null to a None option. But yes, that could potentially happen. IIRC that problem exists in many languages, including F# and Scala. Not sure how big of an issue it is in practice...
- steveklabnik 9y agoTranslated to today's Rust, this code looks like: fn get_cute_cat(img: image_view) -> Option<image_view> { let cropped = find_cat(img)?; let with_tie = add_bow_tie(cropped)?; let with_sparkles = make_eyes_sparkle(with_tie)?; add_rainbow(make_smaller(with_sparkles)) } Basically, with the same amount of boilerplate as your exception-based solution, just three ?s. If you prefer, you could also do fn get_cute_cat(img: image_view) -> Option<image_view> { find_cat(img).and_then(|cropped| add_bow_tie(cropped).and_then(|with_tie| make_eyes_sparkle(with_tie).and_then(|with_sparkles)| add_rainbow(make_smaller(with_sparkles)) ) ) ) } ... but that has rightward drift issues. I _think_ you could do fn get_cute_cat(img: image_view) -> Option<image_view> { find_cat(img) .and_then(add_bow_tie) .and_then(make_eyes_sparkle) .and_then(|with_sparkles| add_rainbow(make_smaller(with_sparkles)) ) ) } but I'm not 100% sure without a comparable example. I bring this up because I don't believe the thing you're seeing is inherent to this style of error-handling, but instead to the language's support for these kinds of monadic patterns. Haskell would look even more succinct than the Rust!
- jwatte 9y agoWelcome to Monad Burger. Would you like a fmap with that?
- kthielen 9y agoI think that this dichotomy can be summarized with the old logical identity: A or B = not(A) implies B Considered as a statement about types, 'A or B' is a variant type over two constructors A/B (this is equivalent to an optional type, where A=unit). Also as a statement about types, 'not(A) implies B' is a function taking a continuation on A (the exception handler) and returning a B (if it decides not to raise an exception). So (as we often find in CS) this is a very old idea, older than computers and programming languages. :D