Skip to content

Commit

Permalink
option icomparable
Browse files Browse the repository at this point in the history
  • Loading branch information
pimbrouwers committed Dec 7, 2024
1 parent 488559c commit 2b92095
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Install solution dependencies
run: dotnet restore --force-evaluate --no-http-cache --force
run: dotnet restore src/Danom.Mvc --force-evaluate --no-http-cache --force

- name: Build Core
run: dotnet build src/Danom -c Release
Expand Down
55 changes: 54 additions & 1 deletion src/Danom/Option/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Danom;
/// </summary>
/// <typeparam name="T"></typeparam>
public readonly struct Option<T>
: IEquatable<Option<T>>
: IEquatable<Option<T>>, IComparable<Option<T>>
{
private readonly T? _some = default;

Expand Down Expand Up @@ -208,6 +208,42 @@ public static Task<Option<T>> NoneAsync() =>
public static bool operator !=(Option<T> left, Option<T> right) =>
!(left == right);

/// <summary>
/// Returns true if the specified <see cref="Option{T}"/> is less than the other.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator <(Option<T> left, Option<T> right) =>
left.CompareTo(right) < 0;

/// <summary>
/// Returns true if the specified <see cref="Option{T}"/> is less than or equal to the other.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator <=(Option<T> left, Option<T> right) =>
left.CompareTo(right) <= 0;

/// <summary>
/// Returns true if the specified <see cref="Option{T}"/> is greater than the other.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator >(Option<T> left, Option<T> right) =>
left.CompareTo(right) > 0;

/// <summary>
/// Returns true if the specified <see cref="Option{T}"/> is greater than or equal to the other.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator >=(Option<T> left, Option<T> right) =>
left.CompareTo(right) >= 0;

/// <summary>
/// Returns true if the specified <see cref="Option{T}"/>s are equal.
/// </summary>
Expand Down Expand Up @@ -242,6 +278,23 @@ public override int GetHashCode() =>
some: x => x is null ? 0 : x.GetHashCode(),
none: () => 0);

/// <summary>
/// Compares the <see cref="Option{T}"/> to another <see cref="Option{T}"/>.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(Option<T> other) =>
Match(
some: x1 =>
other.Match(
some: x2 => Comparer<T>.Default.Compare(x1, x2),
none: () => 1),
none: () =>
other.Match(
some: _ => -1,
none: () => 0)
);

/// <summary>
/// Returns the string representation of the <see cref="Option{T}"/>.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions test/Danom.Tests/Option/OptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,28 @@ public void ToStringDefaultOrFormat()
Assert.NotEqual("0", Option<decimal>.Some(1.9878565765675M).ToString("0", "C2"));
Assert.Equal("£1.99", Option<decimal>.Some(1.9878565765675M).ToString("0", "C2", CultureInfo.CreateSpecificCulture("en-GB")));
}

#pragma warning disable CS1718 // Comparison made to same variable
[Fact]
public void Comparability()
{
var some1 = Option<int>.Some(1);
var some2 = Option<int>.Some(2);
var none = Option<int>.None();

Assert.True(some1 < some2);
Assert.True(some1 <= some2);
Assert.True(some2 > some1);
Assert.True(some2 >= some1);
Assert.True(some1 <= some1);
Assert.True(some1 >= some1);

Assert.True(none < some1);
Assert.True(none <= some1);
Assert.True(some1 > none);
Assert.True(some1 >= none);
Assert.True(none <= none);
Assert.True(none >= none);
}
}
#pragma warning restore CS1718 // Comparison made to same variable

0 comments on commit 2b92095

Please sign in to comment.