3 ms·
Yeah. In the C++ codebase I work on, we use folly::Expected<T, E> (essentially similar to Rust's Result<T, E>) heavily. Is it as ergonomic as Result is in Rus
by loeg 4y ago
Yeah. In the C++ codebase I work on, we use folly::Expected<T, E> (essentially similar to Rust's Result<T, E>) heavily. Is it as ergonomic as Result is in Rust? Absolutely not. But it's still a nice design pattern for some of the same reasons people use Result in Rust.
(And C++ std::optional<T> is somewhat similar to Rust's Option<T> type.)
https://github.com/facebook/folly/blob/main/folly/Expected.h#L819-L913 https://github.com/facebook/folly/blob/main/folly/Expected.h...
- cmrdporcupine 4y agoSounds like Google/abseil's "StatusOr', but maybe a bit more freefrom? https://abseil.io/docs/cpp/guides/status https://abseil.io/docs/cpp/guides/status
- loeg 4y agoYeah, we use a fixed-E alias of folly::Expected<> in our codebase which is similar to absl::StatusOr. Something like: namespace our_project { class Status { ... }; template <class T> using Expected = folly::Expected<T, Status>; } absl::StatusOr<T> looks a lot like our_project::Expected<T>. This pattern of providing your own error type and aliasing Result is also somewhat common in Rust, I believe.
- joenot443 4y agoSwift has a similar Result type, it’s very handy.
- tcmart14 4y agoSwift on linux is supposed to be decent server side, but as far as I've heard, not much use outside of that and even limited testimonials on that. The bit I have dabbled in Swift, I really like it. I just wish it grew more out of the Apple ecosystem. It has lots of things I like, but considering I can't exactly take it with me to any system and just run with it is why I have not dived to much into it.
- klabb3 4y agoIs swift outside of Apple as fully fledged and performant when it comes to concurrency? Last I checked, a lot of the heavy lifting came from GCD (ie the Apple runtime-scheduler)
- Volt 4y agoGCD is open source and runs on FreeBSD and Linux.
- hiccuphippo 4y agoTake a look at Zig (but it's not stable yet, may take a couple of years), linux is a first class citizen for it. It has `error!type` unions and `?type` optionals and language constructs to deal with them: const number = try parseU64(str, 10); // returns the error const number = parseU64(str, 10) catch 13; // default // more complex stuff if (parseU64(str, 10)) |number| { doSomethingWithNumber(number); } else |err| switch (err) { error.Overflow => { // handle overflow... }, // we promise that InvalidChar won't happen (or crash in debug mode if it does) error.InvalidChar => unreachable, }
- tcmart14 4y agoI've been checking out Zig over the past week. It is a really nice language and I like so far. Just not spending as much time as I'd like in it since it is still pre-1.0, but I am excited for it's 1.0.
- mgraczyk 4y agoWorth mentioning that C++23 adds something very similar to the standard library, called std::expected. https://en.cppreference.com/w/cpp/utility/expected https://en.cppreference.com/w/cpp/utility/expected Unfortunately it does not come with nice syntax to return on error.
- loeg 4y agoNice. > Unfortunately it does not come with nice syntax to return on error. Yeah. We use a C preprocessor macro for this, but it's a little more verbose than question-mark operator.