Converting a Result<TValue> object to Result is straightforward, and can be achieved by calling parameterless AsResult() or ToResult() method of Result<TValue> instance or can be left to implicit operator.
Implicit operator and AsResult() method wraps Failure and Statement objects of the source result.
ToResult() method copies over any Failure information, Errors, Facts and Warnings to output Result.
These types of conversions require a TValue object creation for Ok state of output Result<TValue> object.
There are various overloads of AsResult() and AsResultAsync() extension methods that accepts TValue object factory functions and additional parameters for such conversions. Output result wraps Failure and Statement objects of the source result.
public record Request(string Name);
public record Response(string Reply);
public Result<Response> GetResponse(Request req, CancellationToken ct)
{
Result<string> result = await GetMeAResultOfString(req.Name, ct);
return result.AsResult(x => new Response(x));
}
private async Task<Result<string>> GetMeAResultOfString(string name, CancellationToken ct)
{
//some async stuff
await Task.CompletedTask;
return $"Hello {name}";
}Similarly, there are various overloads of ToResult() and ToResultAsync() extension methods that accepts TValue object factory functions and additional parameters for such conversions. Any Failure information, Errors, Facts and Warnings are automatically copied to output Result.
public Result<Response> GetResponse(Request req, CancellationToken ct)
{
Result<string> result = await GetMeAResultOfString(req.Name, ct);
return result.ToResult(x => new Response(x));
}