Skip to content

Commit

Permalink
Make equality operator work with nulls (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Oct 16, 2023
1 parent 59ddad1 commit ae69b85
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Pgvector/Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public bool Equals(Vector? other)
public override bool Equals(object? obj)
=> obj is Vector vector && Equals(vector);

public static bool operator ==(Vector x, Vector y) => x.Equals(y);
public static bool operator !=(Vector x, Vector y) => !(x == y);
public static bool operator ==(Vector? x, Vector? y)
=> x is null && y is null || x is not null && x.Equals(y);

public static bool operator !=(Vector? x, Vector? y) => !(x == y);

public override int GetHashCode()
{
Expand Down
4 changes: 4 additions & 0 deletions tests/Pgvector.Tests/VectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ public void Equal()

Assert.False(a != b);
Assert.True(a != c);

Assert.False(a == null);
Assert.False(null == a);
Assert.True((Vector?)null == null);
}
}

0 comments on commit ae69b85

Please sign in to comment.