@@ -6,7 +6,7 @@ namespace Danom;
6
6
/// </summary>
7
7
/// <typeparam name="T"></typeparam>
8
8
public readonly struct Option < T >
9
- : IEquatable < Option < T > >
9
+ : IEquatable < Option < T > > , IComparable < Option < T > >
10
10
{
11
11
private readonly T ? _some = default ;
12
12
@@ -208,6 +208,42 @@ public static Task<Option<T>> NoneAsync() =>
208
208
public static bool operator != ( Option < T > left , Option < T > right ) =>
209
209
! ( left == right ) ;
210
210
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
+
211
247
/// <summary>
212
248
/// Returns true if the specified <see cref="Option{T}"/>s are equal.
213
249
/// </summary>
@@ -242,6 +278,23 @@ public override int GetHashCode() =>
242
278
some : x => x is null ? 0 : x . GetHashCode ( ) ,
243
279
none : ( ) => 0 ) ;
244
280
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
+
245
298
/// <summary>
246
299
/// Returns the string representation of the <see cref="Option{T}"/>.
247
300
/// </summary>
0 commit comments