Skip to content

Commit

Permalink
Merge pull request #163 from RHaughton/master
Browse files Browse the repository at this point in the history
Implicit operators for failed Result<T>
  • Loading branch information
altmann authored Nov 9, 2022
2 parents 2c1f0f4 + 0650032 commit 5e5c705
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/FluentResults.Test/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,35 @@ public void CreateErrors_FromListOfErrorsImplicitConversion()
result.Reasons[0].Message.Should().Be("First error message");
result.Reasons[1].Message.Should().Be("Second error message");
}

[Fact]
public void CreateErrorOfT_FromErrorImplicitConversion()
{
var error = new Error("")
.CausedBy("First error message");

Result<string> result = error;

result.IsFailed.Should().Be(true);
result.Reasons.Should().HaveCount(1);
error.Reasons.First().Message.Should().Be("First error message");
}

[Fact]
public void CreateErrorOfT_FromListOfErrorsImplicitConversion()
{
var errors = new List<Error>
{
new Error("First error message"),
new Error("Second error message"),
};

Result<string> result = errors;

result.IsFailed.Should().Be(true);
result.Reasons.Should().HaveCount(2);
result.Reasons[0].Message.Should().Be("First error message");
result.Reasons[1].Message.Should().Be("Second error message");
}
}
}
10 changes: 10 additions & 0 deletions src/FluentResults/Results/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,16 @@ public static implicit operator Result<TValue>(TValue value)

return Result.Ok(value);
}

public static implicit operator Result<TValue>(Error error)
{
return Result.Fail(error);
}

public static implicit operator Result<TValue>(List<Error> errors)
{
return Result.Fail(errors);
}

/// <summary>
/// Deconstruct Result
Expand Down

0 comments on commit 5e5c705

Please sign in to comment.