Hello
I have a function that should be comparing two generic types. If they have Equal implemented, this should be preferred. Here is my code:
type EqualChecker interface {
Equal(EqualChecker) bool
}
//AssertEq adds error to test with stack trace when provided arguments are not equal
//If values of Pointers should be compared use AssertEqPtr
func AssertEq[C comparable](left C, right C, t *testing.T) {
leftE, ok := left.(EqualChecker)
if ok {
rightE, _ := right.(EqualChecker)
if !leftE.Equal(rightE) {
t.Errorf("Not equal: \n left: %+v\nright: %+v\n%s", left, right, debug.Stack())
}
return
}
if left != right {
t.Errorf("Not equal: \n left: %+v\nright: %+v\n%s", left, right, debug.Stack())
}
}
But I get the compilation error:
- invalid operation: cannot use type assertion on type parameter value left (variable of type C constrained by comparable)
Any idea how to solve this?