Skip to content

Commit

Permalink
Removing some nonsense destructors:
Browse files Browse the repository at this point in the history
- I think it doesn't make sense to deconstruct both isSuccess/isFailure (just noisy, one would be enough and promoves uniform usage patterns)
- If we're just interested in that specific property (success or failure, without getting the possible Result or IErrors) then we can just get that property
- So if you're using a destructor on `Result<TValue>` you NEED to get TValue and optionally List<IError>.
- For the non-generic Result (the one that does NOT hold an underlying TValue) the destructor `<bool isSuccess, List<IError> errors>` was moved down from the base class

Refs altmann#217
  • Loading branch information
Drizin committed Aug 3, 2024
1 parent a07f12e commit 8b05914
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 116 deletions.
7 changes: 0 additions & 7 deletions src/FluentResults.Test/Playground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ public async Task Simple()

IEnumerable<Result<int>> results2 = new List<Result<int>>();
Result<IEnumerable<int>> mergedResult2 = results.Merge();

var (isSuccess, isFailed) = Result.Ok();
var (isSuccess0, isFailed0, errors0) = Result.Ok();
var (isSuccess1, isFailed1, value1) = Result.Ok(500);
var (isSuccess2, _, value2) = Result.Ok(500);
var (isSuccess3, _, errors3) = Result.Fail("First error");
var (isSuccess4, _, value4, errors4) = Result.Fail<int>("First error");
}

public void TestExtensions()
Expand Down
30 changes: 4 additions & 26 deletions src/FluentResults.Test/ResultWithValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,26 +1015,6 @@ public void Can_deconstruct_generic_Fail_to_isSuccess_and_errors()
errors.Should().NotBeNullOrEmpty();
}

[Fact]
public void Can_deconstruct_generic_Ok_to_isSuccess_and_isFailed_and_value()
{
var (isSuccess, isFailed, value) = Result.Ok(100);

isSuccess.Should().Be(true);
isFailed.Should().Be(false);
value.Should().Be(100);
}

[Fact]
public void Can_deconstruct_generic_Fail_to_isSuccess_and_isFailed_and_value()
{
var (isSuccess, isFailed, value) = Result.Fail<int>("fail");

isSuccess.Should().Be(false);
isFailed.Should().Be(true);
value.Should().Be(default);
}

[Fact]
public void Can_deconstruct_generic_Ok_to_value_with_errors()
{
Expand All @@ -1059,25 +1039,23 @@ public void Can_deconstruct_generic_Fail_to_errors_with_value()


[Fact]
public void Can_deconstruct_generic_Ok_to_isSuccess_and_isFailed_and_value_with_errors()
public void Can_deconstruct_generic_Ok_to_isSuccess_and_value_with_errors()
{
var (isSuccess, isFailed, value, errors) = Result.Ok(100);
var (isSuccess, value, errors) = Result.Ok(100);

isSuccess.Should().Be(true);
isFailed.Should().Be(false);
value.Should().Be(100);
errors.Should().BeNull();
}

[Fact]
public void Can_deconstruct_generic_Fail_to_isSuccess_and_isFailed_and_errors_with_value()
public void Can_deconstruct_generic_Fail_to_isSuccess_and_errors_with_value()
{
var error = new Error("fail");

var (isSuccess, isFailed, value, errors) = Result.Fail<bool>(error);
var (isSuccess, value, errors) = Result.Fail<bool>(error);

isSuccess.Should().Be(false);
isFailed.Should().Be(true);
value.Should().Be(default);

errors.Count.Should().Be(1);
Expand Down
41 changes: 0 additions & 41 deletions src/FluentResults.Test/ResultWithoutValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,47 +603,6 @@ public async Task Try_execute_withresult_failed_valuetask_async_action_with_cust
error.Message.Should().Be("xy");
}

[Fact]
public void Can_deconstruct_non_generic_Ok_to_isSuccess_and_isFailed()
{
var (isSuccess, isFailed) = Result.Ok();

isSuccess.Should().Be(true);
isFailed.Should().Be(false);
}

[Fact]
public void Can_deconstruct_non_generic_Fail_to_isSuccess_and_isFailed()
{
var (isSuccess, isFailed) = Result.Fail("fail");

isSuccess.Should().Be(false);
isFailed.Should().Be(true);
}

[Fact]
public void Can_deconstruct_non_generic_Ok_to_isSuccess_and_isFailed_and_errors()
{
var (isSuccess, isFailed, errors) = Result.Ok();

isSuccess.Should().Be(true);
isFailed.Should().Be(false);
errors.Should().BeNull();
}

[Fact]
public void Can_deconstruct_non_generic_Fail_to_isSuccess_and_isFailed_and_errors()
{
var error = new Error("fail");
var (isSuccess, isFailed, errors) = Result.Fail(error);

isSuccess.Should().Be(false);
isFailed.Should().Be(true);

errors.Count.Should().Be(1);
errors.FirstOrDefault().Should().Be(error);
}

public class BindMethod
{
[Fact]
Expand Down
33 changes: 15 additions & 18 deletions src/FluentResults/Results/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
namespace FluentResults
{
/// <summary>
/// Implementation of a Result
/// A Result that does NOT contain and underlying value.
/// If you want it to hold an underlying value use generic type <see cref="Result{TValue}"/>
/// </summary>
public partial class Result : ResultBase<Result>
{
Expand Down Expand Up @@ -196,7 +197,18 @@ public async ValueTask<Result> Bind(Func<ValueTask<Result>> action)

return result;
}


/// <summary>
/// Deconstruct Result
/// </summary>
/// <param name="isSuccess"></param>
/// <param name="errors"></param>
public void Deconstruct(out bool isSuccess, out List<IError> errors)
{
isSuccess = IsSuccess;
errors = IsFailed ? Errors : default;
}

/// <summary>
/// Implict conversion from <see cref="Error"/> to a <see cref="Result"/>
/// </summary>
Expand Down Expand Up @@ -539,26 +551,11 @@ public static implicit operator Result<TValue>(List<Error> errors)
/// Deconstruct Result
/// </summary>
/// <param name="isSuccess"></param>
/// <param name="isFailed"></param>
/// <param name="value"></param>
public void Deconstruct(out bool isSuccess, out bool isFailed, out TValue value)
{
isSuccess = IsSuccess;
isFailed = IsFailed;
value = IsSuccess ? Value : default;
}

/// <summary>
/// Deconstruct Result
/// </summary>
/// <param name="isSuccess"></param>
/// <param name="isFailed"></param>
/// <param name="value"></param>
/// <param name="errors"></param>
public void Deconstruct(out bool isSuccess, out bool isFailed, out TValue value, out List<IError> errors)
public void Deconstruct(out bool isSuccess, out TValue value, out List<IError> errors)
{
isSuccess = IsSuccess;
isFailed = IsFailed;
value = IsSuccess ? Value : default;
errors = IsFailed ? Errors : default;
}
Expand Down
24 changes: 0 additions & 24 deletions src/FluentResults/Results/ResultBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,30 +213,6 @@ public bool HasSuccess(Func<ISuccess, bool> predicate)
{
return ResultHelper.HasSuccess(Successes, predicate, out _);
}

/// <summary>
/// Deconstruct Result
/// </summary>
/// <param name="isSuccess"></param>
/// <param name="isFailed"></param>
public void Deconstruct(out bool isSuccess, out bool isFailed)
{
isSuccess = IsSuccess;
isFailed = IsFailed;
}

/// <summary>
/// Deconstruct Result
/// </summary>
/// <param name="isSuccess"></param>
/// <param name="isFailed"></param>
/// <param name="errors"></param>
public void Deconstruct(out bool isSuccess, out bool isFailed, out List<IError> errors)
{
isSuccess = IsSuccess;
isFailed = IsFailed;
errors = IsFailed ? Errors : default;
}
}

/// <summary>
Expand Down

0 comments on commit 8b05914

Please sign in to comment.