Skip to content

Commit d4b3b5e

Browse files
committed
Added ! operator for all monad types (related to #261).
1 parent 681402e commit d4b3b5e

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/DotNext.Tests/OptionalTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,27 @@ public static async Task FlattenTask()
359359
await ThrowsAsync<InvalidOperationException>(static () => Task.FromResult(Optional.None<int>()).Flatten());
360360
Equal(42, await Task.FromResult(Optional.Some(42)).Flatten());
361361
}
362+
363+
[Fact]
364+
public static void MiscOperators()
365+
{
366+
Optional<int> result = 20;
367+
False(!result);
368+
369+
if (result)
370+
{
371+
}
372+
else
373+
{
374+
Fail("Optional has no value");
375+
}
376+
377+
result = Optional<int>.None;
378+
True(!result);
379+
380+
if (result)
381+
{
382+
Fail("Optional has value");
383+
}
384+
}
362385
}

src/DotNext/IOptionMonad.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,14 @@ public static virtual bool operator true(in TSelf container)
8484
/// </summary>
8585
/// <param name="container">The container to check.</param>
8686
/// <returns><see langword="true"/> if this container has no value; otherwise, <see langword="false"/>.</returns>
87-
public static virtual bool operator false(in TSelf container)
88-
=> container.HasValue is false;
87+
public static virtual bool operator false(in TSelf container) => !container;
88+
89+
/// <summary>
90+
/// Checks whether the container has no value.
91+
/// </summary>
92+
/// <param name="container">The container to check.</param>
93+
/// <returns><see langword="true"/> if this container has no value; otherwise, <see langword="false"/>.</returns>
94+
public static virtual bool operator !(in TSelf container) => container.HasValue is false;
8995

9096
/// <summary>
9197
/// Returns the value if present; otherwise return default value.

src/DotNext/Optional.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,16 +772,22 @@ public bool Equals(object? other, IEqualityComparer comparer)
772772
/// </summary>
773773
/// <param name="optional">The container to check.</param>
774774
/// <returns><see langword="true"/> if this container has value; otherwise, <see langword="false"/>.</returns>
775-
/// <see cref="HasValue"/>
776-
[MemberNotNullWhen(true, nameof(ValueOrDefault))]
775+
/// <seealso cref="HasValue"/>
777776
public static bool operator true(in Optional<T> optional) => optional.HasValue;
778777

779778
/// <summary>
780779
/// Checks whether the container has no value.
781780
/// </summary>
782781
/// <param name="optional">The container to check.</param>
783782
/// <returns><see langword="true"/> if this container has no value; otherwise, <see langword="false"/>.</returns>
784-
/// <see cref="HasValue"/>
785-
[MemberNotNullWhen(false, nameof(ValueOrDefault))]
786-
public static bool operator false(in Optional<T> optional) => optional.kind < NotEmptyValue;
783+
/// <seealso cref="HasValue"/>
784+
public static bool operator false(in Optional<T> optional) => !optional;
785+
786+
/// <summary>
787+
/// Checks whether the container has no value.
788+
/// </summary>
789+
/// <param name="optional">The container to check.</param>
790+
/// <returns><see langword="true"/> if this container has no value; otherwise, <see langword="false"/>.</returns>
791+
/// <seealso cref="HasValue"/>
792+
public static bool operator !(in Optional<T> optional) => optional.kind < NotEmptyValue;
787793
}

0 commit comments

Comments
 (0)