Giter Site home page Giter Site logo

fisothemes / result-cpp Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 213 KB

A lightweight, header-only C++ library that provides a result type for monadic error handling .

License: MIT License

CMake 8.38% C++ 91.62%
cpp cpp17 error error-handling expected expected-value result header-only

result-cpp's Introduction

Result-CPP

Result-CPP is a lightweight, header-only C++ library for error handling using the monadic pattern in C++. It provides a generic class called result that can store either a successful value or an error value as well as ways to extract and handle them.

Features

  • Simple and concise result type for error handling.
  • Header-only library with no external dependencies.
  • Supports chaining operations and handling errors.

Getting Started

Prerequisites

  • C++17 compiler

Usage

  1. Include the result.hpp header in your C++ project.
  2. Start using the fst::result type for handling success and error states.

Example

#include <iostream>
#include <limits>

#include "fst/result.hpp"

// Function that may fail and return a Result
fst::result<double, std::string> div(double a, double b) {
  if (b == 0.0)
    return std::string("Division by zero error");  // Implicitly converts to std::string
  return a / b;  // Implicitly converts to double
}

int main() {
// Example 1: Chaining with `and_then` and `map`
auto result1 = div(12.0, 3.0)
                    .and_then([](const auto& x) {
                      return x > 0.0 
                          ? fst::result<double, std::string>(x * 2.0) 
                          : fst::result<double, std::string>("Negative result");
                    })
                    .map([](const auto& y) {
                      std::cout << "Result: " + std::to_string(y) << '\n';
                      return y*y;
                    });

std::cout << "Example 1: " << result1 << '\n';

// Example 2: Chaining with `or_else` and `map_error`
auto result2 = div(10.0, 0.0)
                    .or_else([](const auto& error) {
                      return fst::result<double, std::string>("Error: " + error);
                    })
                    .map_error([](const std::string& original_error) {
                      return original_error + " (mapped)";
                    });

std::cout << "Example 2: " << result2 << '\n';

// Example 3: Chaining with `transform`
auto result3 = div(20.0, 4.0)
                    .transform([](const auto& res) {
                      return res.success() 
                          ? fst::result<std::string, std::string>(
                                fst::success_t, 
                                "Success! No error") 
                          : fst::result<std::string, std::string>(
                                fst::error_t,
                                res.error()
                                   .value_or("Unknown error"));
                    });

std::cout << "Example 3: " << result3 << '\n';

// Example 4: Using `inspect` for side effects
auto result4 = div(8.0, 2.0)
                   .inspect([](const fst::result<double, std::string>& res) {
                     if (auto value = res.success()) {
                       std::cout << "Success! Result is: " 
                                 << *value 
                                 << '\n';
                     } else {
                       std::cerr << "Error! Message: " 
                                 << res.error()
                                       .value_or("Unknown error") 
                                 << '\n';
                     }
                   });

 std::cout << "Example 4: " << result4 << '\n';

  return 0;
}

result-cpp's People

Contributors

fisothemes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.