Skip to content

Commit

Permalink
Improve/simplify Optional EqualityComparer GetHashCode impl
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Jan 5, 2025
1 parent 5646fd9 commit 2b016fb
Showing 1 changed file with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.CompilerServices;

namespace System;
namespace System;

partial struct Optional<T>
{
Expand All @@ -22,19 +20,14 @@ public bool Equals(Optional<T> x, Optional<T> y)
}

public int GetHashCode(Optional<T> obj)
=>
obj.hasValue ? PresentHashCode(obj.value) : AbsentHashCode();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int PresentHashCode(T value)
=>
value is not null
? HashCode.Combine(true, comparer.GetHashCode(value))
: HashCode.Combine(true);
{
if (obj.hasValue)
{
return HashCode.Combine(
obj.value is null ? default : comparer.GetHashCode(obj.value));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int AbsentHashCode()
=>
HashCode.Combine(false);
return default;
}
}
}

0 comments on commit 2b016fb

Please sign in to comment.