map<U> method

Result<U, E> map<U>(
  1. U op(
    1. T val
    )
)

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

This function can be used to compose the results of two functions.

Example

Basic Usage

import 'package:dartsult/dartsult.dart';

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

  Result<int, String> x = Result.ok<int, String>(13);
  assert(x.map(stringify) == Result.ok<String, String>('code: 13'));
}

Implementation

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