Skip to content

Commit 06addcd

Browse files
authored
Merge pull request #261 from Dixin/result
Add ! operator overloading for the result types.
2 parents d46cece + b335fff commit 06addcd

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/DotNext.Tests/ResultTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ public static void Operators()
7272
var result = new Result<int>(10);
7373
if (result) { }
7474
else Fail("Unexpected Result state");
75+
if (!result) Fail("Unexpected Result state");
7576
Equal(10, (int)result);
7677
Equal("10", result.ToString());
7778
Optional<int> opt = result;
7879
Equal(10, opt);
7980
Equal(10, result.OrInvoke(static () => 20));
8081
result = new Result<int>(new Exception());
82+
if (result) Fail("Unexpected Result state");
83+
if (!result) { }
84+
else Fail("Unexpected Result state");
8185
Equal(20, result.OrInvoke(static () => 20));
8286
opt = result;
8387
False(opt.HasValue);
@@ -89,12 +93,16 @@ public static void Operators2()
8993
var result = new Result<int, EnvironmentVariableTarget>(10);
9094
if (result) { }
9195
else Fail("Unexpected Result state");
96+
if (!result) Fail("Unexpected Result state");
9297
Equal(10, (int)result);
9398
Equal("10", result.ToString());
9499
Optional<int> opt = result;
95100
Equal(10, opt);
96101
Equal(10, result.OrInvoke(static () => 20));
97102
result = new Result<int, EnvironmentVariableTarget>(EnvironmentVariableTarget.Machine);
103+
if (result) Fail("Unexpected Result state");
104+
if (!result) { }
105+
else Fail("Unexpected Result state");
98106
Equal(20, result.OrInvoke(static () => 20));
99107
opt = result;
100108
False(opt.HasValue);

src/DotNext/Result.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,13 @@ public ValueTask<T> AsTask()
376376
/// <returns><see langword="true"/> if both results are successful; otherwise, <see langword="false"/>.</returns>
377377
public static bool operator &(in Result<T> left, in Result<T> right) => left.exception is null && right.exception is null;
378378

379+
/// <summary>
380+
/// Indicates that the result represents error.
381+
/// </summary>
382+
/// <param name="result">The result to check.</param>
383+
/// <returns><see langword="false"/> if this result is successful; <see langword="true"/> if this result represents exception.</returns>
384+
public static bool operator !(in Result<T> result) => result.exception is not null;
385+
379386
/// <summary>
380387
/// Indicates that the result is successful.
381388
/// </summary>
@@ -699,6 +706,13 @@ public unsafe T OrThrow(delegate*<TError, Exception> exceptionFactory)
699706
/// <returns><see langword="true"/> if both results are successful; otherwise, <see langword="false"/>.</returns>
700707
public static bool operator &(in Result<T, TError> left, in Result<T, TError> right) => left.IsSuccessful && right.IsSuccessful;
701708

709+
/// <summary>
710+
/// Indicates that the result represents error.
711+
/// </summary>
712+
/// <param name="result">The result to check.</param>
713+
/// <returns><see langword="false"/> if this result is successful; <see langword="true"/> if this result represents exception.</returns>
714+
public static bool operator !(in Result<T, TError> result) => !result.IsSuccessful;
715+
702716
/// <summary>
703717
/// Indicates that the result is successful.
704718
/// </summary>

0 commit comments

Comments
 (0)