Try<T> function

Validation<T> Try<T>(
  1. T tryBlock()
)

Implementation

Validation<T> Try<T>(T Function () tryBlock) {
  try
  {
    return Valid<T>(tryBlock());
  }
  catch(e)
  {
    if(e is Error)
    {
      return Fail.withError(e).toInvalid<T>();
    }
    else if(e is Exception) {
      return Fail.withException(e).toInvalid<T>();
    }

    return Fail.withError(Error(), message: 'Unknown error').toInvalid<T>();
  }
}