Try<T> static method

Validation<T> Try<T>(
  1. T f(), {
  2. String failMessage = '',
})

Implementation

static Validation<T> Try<T>(T Function() f, {String failMessage = ''}) {
  try{
    return Valid(f());
  }
  catch (e)
  {
    final fail = e is Exception ? Fail.withException(e, message: failMessage)
                                : Fail.withError(e as Error, message: failMessage);
    return Invalid<T>([fail]);
  }
}