Result

Description: For a detailed explanation of how Result and Promise work, see this blog post. But keep in mind that the API has changed since that was written. The Match(), MatchVal() and MatchErr() functions described in the blog post are now entirely replaced with Then() and Catch().

A Result represents a value that may or may not exist. If it doesn't exist, it houses an Error explaining why. It's generic over the type of the value. In order to maintain type safety, the contents of the Result are private. They're accessed through lambdas with the Then() or Catch() methods.

In proper computer science terms: Result is a sum type, with simulated pattern matching through the Then() and Catch() methods.

Methods:

  • static Result<ValueT> Val(ValueT val) - This factory function creates a new Result with the given value.
  • static Result<ValueT> Err(Errorerr) - This factory function creates a new Result with the given Error.
  • bool HasValue() - Returns true if the Result currently contains a value, or false if it contains an Error.
  • bool HasError() - Returns true if the Result currently contains an Error, or false if it contains a value.
  • void Then(Action<ValueT> valFunc, Action<Error> errFunc) - If the Result contains a value, call the valFunc with that value. If it contains an Error, call the errFunc with that Error.
  • void Then(Action<ValueT> valFunc) - If the Result contains a value, call the valFunc with that value.
  • RetT Then<RetT>(Func<ValueT, RetT> valFunc, Func<Error, RetT> errFunc) - If the Result contains a value, call the valFunc with that value, and return it's return value. If it contains an Error, call the errFunc with that Error, and return it's return value.
  • RetT Then<RetT>(Func<ValueT, RetT> valFunc, RetT defaultValue) - If the Result contains a value, call the valFunc with that value, and return it's return value. If it contains an Error, return the defaultValue instead.
  • Result<NewValT> Then<NewValT>(Func<ValueT, Result<NewValT>> valFunc) - If the Result contains a value, call the valFunc with that value, and return the Result it returns. If it contains an Error, create a new Result containing the same Error and return that.
  • Promise<NewValT> Then<NewValT>(Func<ValueT,Promise<NewValT>> valFunc) - If the Result contains a value, call the valFunc with that value, and return the Promise it returns. If it contains an Error, create a new Promise containing the same Error and return that.
  • void Catch(Action<Error> errFunc) - If the Result contains an Error, call the errFunc with that Error.
  • RetT Catch<RetT>(Func<Error, RetT> errFunc, RetT defaultError) - If the Result contains an Error, call the errFunc with that value, and return it's return value. If it contains a value, return the defaultError instead.
  • Result<ValueT> Catch(Func<Error, Result<ValueT>> errFunc) - If the Result contains an Error, call the errFunc with that Error, and return the Result it returns. If it contains a value, create a new Result containing the same value and return that.
  • Promise<ValueT> Catch(Func<Error,Promise<ValueT>> errFunc) - If the Result contains an Error, call the errFunc with that Error, and return the Promise it returns. If it contains a value, create a new Promise containing the same value and return that.