Conversion of Results
See original GitHub issueHi,
I do a lot of conversion of results. My problem is that the following code is not very readable, especially the part Result.Fail<MyValueClass>(result1.Error).
public Result<MyValueClass> DoSomething()
{
Result result1 = MyMethod();
if(result1.IsFailure)
return Result.Fail<MyValueClass>(result1.Error);
[...]
}
Do you know a simple conversion? If not, I would suggest a new conversion method between results ToResult(). With this conversion method the above code looks like this:
public Result<MyValueClass> DoSomething()
{
Result result1 = MyMethod();
if(result1.IsFailure)
return result1.ToResult<MyValueClass>();
[...]
}
With ToResult() the already existing implicit operator from Result< T > to Result the conversion between Result and Result< T > is enabled and vice versa.
I only see one open problem with the conversion from an success Result to Result< T >. Result<T> result = Result.Ok().ToResult<T>() Which Value has now variable result? default(T)? Or should this conversion throw an exception?
The conversion from an failed Result to Result< T > makes no problems. Result<T> result = Result.Fail("Failed").ToResult<T>() Value is not set.
What do you think?
BR Michael
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (8 by maintainers)

Top Related StackOverflow Question
I’m with @altmann on this one. Not all operations can be easily represented with a chain of OnSuccess methods, for example, embedded
OnSuccess’s are quite hard to grasp and I personally prefer to fall back to regularif-elseinstead.MapFailurewould be really useful in those cases.Yes, in 99% I need this conversion in situations which I have to convert a failed result to another type of result.
I will have a look to Map(…) and send you an PR with MapFailure(…).
Thanks…