Skip to content

Commit cd01572

Browse files
authored
Tweak Score and ScoredValue (#617)
### Motivation and Context A bit of cleanup for the `Score` and `ScoredValue` types. ### Description - Make them both readonly, removing the setters from properties - Give Score a public ctor - Make the Min static properties internal (it doesn't really make sense on ScoredValue, and it's strange to have a Min but no Max) - Make ScoredValue's implicit operators for casting to T or double to instead be explicit; implicit operators shouldn't be lossy, but by definition throwing away half of the state is lossy - Remove unnecessary null checks
1 parent c9b9220 commit cd01572

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

dotnet/src/SemanticKernel/Memory/Collections/Score.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ namespace Microsoft.SemanticKernel.Memory.Collections;
88
/// <summary>
99
/// Structure for storing score value.
1010
/// </summary>
11-
public struct Score : IComparable<Score>, IEquatable<Score>
11+
public readonly struct Score : IComparable<Score>, IEquatable<Score>
1212
{
13-
public double Value { get; set; }
13+
public double Value { get; }
1414

15-
public static Score Min => double.MinValue;
15+
public Score(double value)
16+
{
17+
this.Value = value;
18+
}
19+
20+
internal static Score Min => double.MinValue;
1621

1722
public static implicit operator Score(double score)
1823
{
19-
return new Score { Value = score };
24+
return new Score(score);
2025
}
2126

2227
public static implicit operator double(Score src)

dotnet/src/SemanticKernel/Memory/Collections/ScoredValue.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ namespace Microsoft.SemanticKernel.Memory.Collections;
99
/// Structure for storing data which can be scored.
1010
/// </summary>
1111
/// <typeparam name="T">Data type.</typeparam>
12-
public struct ScoredValue<T> : IComparable<ScoredValue<T>>, IEquatable<ScoredValue<T>>
12+
public readonly struct ScoredValue<T> : IComparable<ScoredValue<T>>, IEquatable<ScoredValue<T>>
1313
{
1414
public ScoredValue(T item, double score)
1515
{
1616
this.Value = item;
1717
this.Score = score;
1818
}
1919

20-
public T Value { get; private set; }
21-
public Score Score { get; private set; }
20+
public T Value { get; }
21+
public Score Score { get; }
2222

2323
public int CompareTo(ScoredValue<T> other)
2424
{
@@ -30,12 +30,12 @@ public override string ToString()
3030
return $"{this.Score}, {this.Value}";
3131
}
3232

33-
public static implicit operator double(ScoredValue<T> src)
33+
public static explicit operator double(ScoredValue<T> src)
3434
{
3535
return src.Score;
3636
}
3737

38-
public static implicit operator T(ScoredValue<T> src)
38+
public static explicit operator T(ScoredValue<T> src)
3939
{
4040
return src.Value;
4141
}
@@ -52,9 +52,8 @@ public override bool Equals(object obj)
5252

5353
public bool Equals(ScoredValue<T> other)
5454
{
55-
return (other != null)
56-
&& (this.Value?.Equals(other.Value) == true)
57-
&& (this.Score.Equals(other.Score));
55+
return EqualityComparer<T>.Default.Equals(other.Value) &&
56+
this.Score.Equals(other.Score);
5857
}
5958

6059
public override int GetHashCode()
@@ -92,9 +91,7 @@ public override int GetHashCode()
9291
return left.CompareTo(right) >= 0;
9392
}
9493

95-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1000:Do not declare static members on generic types",
96-
Justification = "Min value convenience method")]
97-
public static ScoredValue<T> Min()
94+
internal static ScoredValue<T> Min()
9895
{
9996
return new ScoredValue<T>(default!, Score.Min);
10097
}

0 commit comments

Comments
 (0)