Skip to content

Commit 2b92095

Browse files
committed
option icomparable
1 parent 488559c commit 2b92095

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
dotnet-version: ${{ matrix.dotnet-version }}
2121

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

2525
- name: Build Core
2626
run: dotnet build src/Danom -c Release

src/Danom/Option/Option.cs

+54-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Danom;
66
/// </summary>
77
/// <typeparam name="T"></typeparam>
88
public readonly struct Option<T>
9-
: IEquatable<Option<T>>
9+
: IEquatable<Option<T>>, IComparable<Option<T>>
1010
{
1111
private readonly T? _some = default;
1212

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

211+
/// <summary>
212+
/// Returns true if the specified <see cref="Option{T}"/> is less than the other.
213+
/// </summary>
214+
/// <param name="left"></param>
215+
/// <param name="right"></param>
216+
/// <returns></returns>
217+
public static bool operator <(Option<T> left, Option<T> right) =>
218+
left.CompareTo(right) < 0;
219+
220+
/// <summary>
221+
/// Returns true if the specified <see cref="Option{T}"/> is less than or equal to the other.
222+
/// </summary>
223+
/// <param name="left"></param>
224+
/// <param name="right"></param>
225+
/// <returns></returns>
226+
public static bool operator <=(Option<T> left, Option<T> right) =>
227+
left.CompareTo(right) <= 0;
228+
229+
/// <summary>
230+
/// Returns true if the specified <see cref="Option{T}"/> is greater than the other.
231+
/// </summary>
232+
/// <param name="left"></param>
233+
/// <param name="right"></param>
234+
/// <returns></returns>
235+
public static bool operator >(Option<T> left, Option<T> right) =>
236+
left.CompareTo(right) > 0;
237+
238+
/// <summary>
239+
/// Returns true if the specified <see cref="Option{T}"/> is greater than or equal to the other.
240+
/// </summary>
241+
/// <param name="left"></param>
242+
/// <param name="right"></param>
243+
/// <returns></returns>
244+
public static bool operator >=(Option<T> left, Option<T> right) =>
245+
left.CompareTo(right) >= 0;
246+
211247
/// <summary>
212248
/// Returns true if the specified <see cref="Option{T}"/>s are equal.
213249
/// </summary>
@@ -242,6 +278,23 @@ public override int GetHashCode() =>
242278
some: x => x is null ? 0 : x.GetHashCode(),
243279
none: () => 0);
244280

281+
/// <summary>
282+
/// Compares the <see cref="Option{T}"/> to another <see cref="Option{T}"/>.
283+
/// </summary>
284+
/// <param name="other"></param>
285+
/// <returns></returns>
286+
public int CompareTo(Option<T> other) =>
287+
Match(
288+
some: x1 =>
289+
other.Match(
290+
some: x2 => Comparer<T>.Default.Compare(x1, x2),
291+
none: () => 1),
292+
none: () =>
293+
other.Match(
294+
some: _ => -1,
295+
none: () => 0)
296+
);
297+
245298
/// <summary>
246299
/// Returns the string representation of the <see cref="Option{T}"/>.
247300
/// </summary>

test/Danom.Tests/Option/OptionTests.cs

+24
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,28 @@ public void ToStringDefaultOrFormat()
205205
Assert.NotEqual("0", Option<decimal>.Some(1.9878565765675M).ToString("0", "C2"));
206206
Assert.Equal("£1.99", Option<decimal>.Some(1.9878565765675M).ToString("0", "C2", CultureInfo.CreateSpecificCulture("en-GB")));
207207
}
208+
209+
#pragma warning disable CS1718 // Comparison made to same variable
210+
[Fact]
211+
public void Comparability()
212+
{
213+
var some1 = Option<int>.Some(1);
214+
var some2 = Option<int>.Some(2);
215+
var none = Option<int>.None();
216+
217+
Assert.True(some1 < some2);
218+
Assert.True(some1 <= some2);
219+
Assert.True(some2 > some1);
220+
Assert.True(some2 >= some1);
221+
Assert.True(some1 <= some1);
222+
Assert.True(some1 >= some1);
223+
224+
Assert.True(none < some1);
225+
Assert.True(none <= some1);
226+
Assert.True(some1 > none);
227+
Assert.True(some1 >= none);
228+
Assert.True(none <= none);
229+
Assert.True(none >= none);
230+
}
208231
}
232+
#pragma warning restore CS1718 // Comparison made to same variable

0 commit comments

Comments
 (0)