mapError<F> method

Result<T, F> mapError<F>(
  1. F op(
    1. E err
    )
)

Maps a Result<T, E> to Result<T, F> by applying a function to a contained error value, leaving an ok value untouched.

This function can be used to pass through a successful result while handling an error.

Example

Basic Usage

import 'package:dartsult/dartsult.dart';

main() {
  String stringify(int x) {
    return 'error code: $x';
  }

  Result<int, int> x = Result.ok(2);
  assert(x.mapError(stringify) == Result.ok<int, String>(2));

  x = Result.error(13);
  assert(x.mapError(stringify) == Result.error<int, String>("error code: 13"));
}

Implementation

Result<T, F> mapError<F>(F Function(E err) op) {
  if (_ok.val != null) {
    return Result.ok(_ok.val!);
  }
  return Result.error(op(_error.val!));
}