PragmaTwice opened a new pull request, #768:
URL: https://github.com/apache/incubator-kvrocks/pull/768

   In kvrocks we use a style like `Status process(input..., T* output)` to 
handle errors. It works well, but in C++ we can do better via constructing 
type-safe union types.
   
   In this PR, we propose a new class template `StatusOr<T>`, which contains 
either a value typed T or a status. Here is a simple example:
   
   ```c++
   StatusOr<std::string> hello(std::string x) {
     if (x.size() > 10) {
       return {Status::NotOK, "string too long"};
     }
   
     return x + " hello";
   };
   
   StatusOr<std::string> hi(std::string x) {
     if (x.size() < 5) {
       return {Status::NotOK, "string too short"};
     }
   
     auto res = f(x);
     if (!res) return res;
   
     return "hi " + *res;
   };
   
   assert(*hi("twice") == "hi twice hello");
   
   auto short = hi("x");
   assert(!short && short.Msg() == "string too short");
   
   auto long= hi("xxxxxxxxxxx");
   assert(!long && long.Msg() == "string too long");
   ```
   
   We maximize the use of move semantics in C++ to eliminate redundant copies 
and optimize the storage, so that developers do not need to worry too much 
about its performance when using it.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to