❔ ✅ Using generics that implement IEqualityOperators results in ambiguous call to the == operator
If I have some
T: IEqualityOperators<T, T, bool>
and use a LINQ query such as myTs.FirstOrDefault(t => t == otherT)
, I get an error saying that the ==
operator call is ambiguous. How can I make sure the equality check happens through the IEqualityOperators
interface and not using the reference equals operator?
Sharplab: https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRj1UwAJUCAWAbmLIoDoAZASygEdb96CGA5CAFsApmBYBjAM5diBAJwAKAEQAJYQBt1AexikA7lrDq4AQiUBKGfgDMpScEjjgpALIBDBywAe6ACIBRKAcATwAeABUAJS09XXCAYS11IVhScIA1N2ThAD48UgL9AAtRYTTovVIQOwcIJ10ASX92CCyWYGCAeQAHUQ9DSQiKuOHSACMtJJzGxMFutzFJLSgevuABoZiRrfKYvNxC4tK0xOTBKCqax2BG5tb1dq7esH6wQYSklLjTr/HJ9WmpAas3mi2Wqxe6zeER+52+nzhJwRUH2hz0JTAZQyWQgZWq9mutxabQ6ENe70y2TilNxugmUxmWjmCxYSxWz3JERpwmpOJ5aW5+wA3vlCqhbO5PD4AkEwMEFFEYqQwDsPmcLuJkbzsqQAG588yigoig6HQoVUgAXmVMS4ZsKsIu1s16rt9tI3Kter5boKAF88EbyLZFZULULSABzYTAaikNjtOMB00FcVI9WkR2kCPR2PxqCJ0jJw5p7E6z05mNxyRVot4ZPEWwYVweMTSwIhU7qYROFjLTaxdO/Mu41GFdHHUOXAl1G5Au4kp5rDah7aD+kAxnMsHs5fQ1e7QehscFCeYofnae1erz4kPUkcqHvR3w9V0/6A4FM0Gs8GPlcvheqRqikJ5HOeI54lcs5Evcjxkk+XJ8tqtJ/AyQIgiybIISu3IofykHCkG3RiPqwBlJibhwMs6jBKQrD2KEkptn4HZygOr7DoKOSkAA+sIsosMI0iBimwYtlKrGysEXY9sAfZQAohpiSa7p8QJnjCV6UDCHoSm+nWuBBqWnrAEUrIANpTiqR5Zi6KQALpBqp7q5laPH8YJwkMAAymwkbdgoZSWjxwgMBalrWjZpAAGQxaQYVZpFpD2ec5gMNyBmHDWwBBocLlqQUIR5e6BWFYc+pgAl0lep5mmSL5/mBcFoXhTEDALuokgKDZ5ixfFiXIh1d7dalUDmJYJXlRpcoZXyXr6tkWXusW5UpR44hFKQCj+F44jCN08nLMpa3ZlNhV1WI3kAIJwHACg6XpNm6GNuiLbiE3Lfaq3fUGyZ+kAASharpLab
C#/VB/F# compiler playground.
12 Replies
show code and show exact error please
hey sorry for the slow reply, I can't reproduce the error on my machine somehow, it happened on my work pc, I'll update my post later
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
SharpLab
C#/VB/F# compiler playground.
Also tagging @tannergooding cause it's a numerics thing
IComparisonOperators
implies IEqualityOperators
so you've defined both IEqualityOperators<TRow, TRow, bool>
and IEqualityOperators<TRow, TRow, TRow>
therefore overloading by return type which is not normally supported
thus C# cannot determine which of the two you wantOh I see, I should've checked
So just comparison operators is enough then
it depends.
How are you expecting
IComparisonOperators
to work given the return type is TRow
and not bool
?Hmm, good question, I guess the element that precedes the other should be returned, similar to how
IComparable
returns its intsThere are some cases where that can make sense, such as
Vector128<T> GreaterThan(Vector128<T>, Vector128<T>)
which return a per element mask
but, you should typically have that as a different API so that the normal usage of x > y
works in typical user expressions/etcI see, I guess if I'm not working with numbers explicitly I could stick to just equality stuff
Thanks!
!close
Closed!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.