Skip to content

Commit

Permalink
Optional Comparison: Add Optional<T> vs T comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Jan 12, 2025
1 parent 174ca49 commit 1152f0c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ internal int InternalCompareTo(Optional<T> other, IComparer<T> comparer)

return ComparisonResult.EqualTo;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal int InternalCompareTo(T other, Comparer<T> comparer)
{
if (hasValue)
{
return ComparisonResult.Normalize(comparer.Compare(value, other));
}

return ComparisonResult.LessThan;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ namespace System;

partial class Optional
{
// TODO: Add the tests and open the method
// TODO: Add the tests and open the methods

internal static int Compare<T>(Optional<T> left, Optional<T> right)
where T : IComparable<T>
=>
left.InternalCompareTo(right, Comparer<T>.Default);

internal static int Compare<T>(Optional<T> left, T right)
where T : IComparable<T>
=>
left.InternalCompareTo(right, Comparer<T>.Default);

internal static int Compare<T>(T left, Optional<T> right)
where T : IComparable<T>
=>
right.InternalCompareTo(left, Comparer<T>.Default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ namespace System;

partial class OptionalExtensions
{
// TODO: Add the tests and open the method
// TODO: Add the tests and open the methods

internal static int CompareTo<T>(this Optional<T> optional, Optional<T> other)
where T : IComparable<T>
=>
optional.InternalCompareTo(other, Comparer<T>.Default);

internal static int CompareTo<T>(this Optional<T> optional, T other)
where T : IComparable<T>
=>
optional.InternalCompareTo(other, Comparer<T>.Default);
}

0 comments on commit 1152f0c

Please sign in to comment.