-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optional Comparison: reduce code coupling
- Loading branch information
Showing
5 changed files
with
36 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...taggeds-optional/Optional/Optional.T/Internal.Comparison/Optional.T.InternalComparison.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System; | ||
|
||
partial struct Optional<T> | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal int InternalCompareTo(Optional<T> other, IComparer<T> comparer) | ||
{ | ||
if (hasValue != other.hasValue) | ||
{ | ||
return hasValue ? ComparisonResult.GreaterThan : ComparisonResult.LessThan; | ||
} | ||
|
||
if (hasValue) | ||
{ | ||
return ComparisonResult.Normalize(comparer.Compare(value, other.value)); | ||
} | ||
|
||
return ComparisonResult.EqualTo; | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
src/core-taggeds-optional/Optional/Optional/Optional.Comparison.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
namespace System; | ||
using System.Collections.Generic; | ||
|
||
namespace System; | ||
|
||
partial class Optional | ||
{ | ||
// TODO: Add the tests and open the method | ||
internal static int Compare<T>(Optional<T> left, Optional<T> right) | ||
where T : IComparable<T> | ||
=> | ||
OptionalComparer<T>.Default.Compare(left, right); | ||
left.InternalCompareTo(right, Comparer<T>.Default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
src/core-taggeds-optional/Optional/OptionalExtensions/Comparison.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
namespace System; | ||
using System.Collections.Generic; | ||
|
||
namespace System; | ||
|
||
partial class OptionalExtensions | ||
{ | ||
// TODO: Add the tests and open the method | ||
internal static int CompareTo<T>(this Optional<T> optional, Optional<T> other) | ||
where T : IComparable<T> | ||
=> | ||
OptionalComparer<T>.Default.Compare(optional, other); | ||
optional.InternalCompareTo(other, Comparer<T>.Default); | ||
} |