Git Product home page Git Product logo

result's Introduction

result

TODO:

Documentation, tests, finish up static_assert contracts, finish up implementation of the less important methods.

  • Appears to be unnecessary copying in ok()/err()

  • option to throw an exception instead of abort on bad ok()/err()? easy propagation?

usage:

Here's a real example taken from example2.cxx showing the power of Result<T,E>:

static void two() {
  std::string contents = util::open("conf.ini", util::openmode::in)
    .context("Failed to load conf.ini")
    .apply(util::as_string)
    .ok("Failed to read file.");

  std::printf("File contents:\n%s", contents.c_str());
}

It makes use of the utils header which provides some adapters for the standard library and common functions, which is a major WIP.

don't do this:

auto&& r = functionThatReturnsResult().context("some context");

it will bind to a reference that is out of scope.

do this:

auto r = functionThatReturnsResult().context("some context");

or this:

auto&& r = functionThatReturnsResult();
r.context("some context");

The temporary that is propagated to context ceases to exist at the end of the full expression containing the call([class.temporary]), and the lifetime is not extended by an rvalue reference/const reference as you may unintuitively believe.

I may change it in the future for rvalue member functions to instead return by value which would make this safe. My only current ideas are providing a wrapper proxy class to mimic the result which holds an rvalue reference to propagate the lifetime of the result without requiring any copies.

general notes:

Don't be afraid of using ok() when first writing your code. Unlike the harmful practice of ignoring an error code, the program will crash as loudly as possible if it contained an error. This allows for rapid prototyping while making bugs more apparent during testing.

Result<T,E> (in my opinion) is meant to be dealt with at the call site. It's not a replacement for exceptions(don't hit me with your shoe please,) it's an alternative. I came to this decision when studying Rust's error handling capabilities and their need for crates like error_chain to emulate what exceptions already do. Result<T,E> fills a similar role as when you'd return an optional(Optional<T> is just Result<T,void>.) Maybe in the future I'll examine something like error_chain's functionality'.

Thus, a Result<T,E> should be preferred for errors you can handle, and exceptions for errors that you cannot and need to propagate up the call stack.

implementation notes:

The Try_ macro requires a GCC and Clang specific extension. I'll have to think of a way to write it without statement expressions. This is one of the few areas where statement expressions can't be replaced by a do-while(0) or lambda. Maybe exceptions could be used here. Not exactly a priority as I don't really care about MSVC though.

result's People

Watchers

James Cloos avatar  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.