transf<R extends Object> method

  1. @override
Result<R> transf<R extends Object>([
  1. @noFutures R noFutures(
    1. T e
    )?
])
override

Transforms the Outcome's generic type from T to R.

Uses the transformer function noFutures if provided, otherwise attempts a direct cast.

Implementation

@override
Result<R> transf<R extends Object>([@noFutures R Function(T e)? noFutures]) {
  try {
    final a = value;
    final b = noFutures?.call(a) ?? a as R;
    return Ok(b);
  } on Err catch (err) {
    // If the user-supplied transformer throws an `Err`, preserve it
    // verbatim — wrapping it as a string in another Err would discard the
    // statusCode/breadcrumbs that life-critical callers may rely on.
    return err.transfErr<R>();
  } catch (error, stackTrace) {
    return Err(
      'Cannot transform $T to $R: $error',
      stackTrace: stackTrace,
    );
  }
}