Closed
Description
What do I want?
- I want a new
comparable
type that does not includeinterface{}
.
Background
- The current
comparable
type includesinterface{}
. - If an
interface{}
is passed to the function, we need to determine if it is comparable at runtime using reflect package. (to avoid panic)- See examples
I think comparable
type which can only be determined at runtime, is almost the same as any
type.
generics
import "reflect"
func Equal[T comparable](v1, v2 T) bool {
if !reflect.TypeOf(v1).Comparable() {
return false
}
if !reflect.TypeOf(v2).Comparable() {
return false
}
return v1 == v2
}
func main() {
v1 := interface{}(func() {})
v2 := interface{}(func() {})
Equal(v1, v2)
}
non-generics
import "reflect"
func Equal(v1, v2 interface{}) bool {
if !reflect.TypeOf(v1).Comparable() {
return false
}
if !reflect.TypeOf(v2).Comparable() {
return false
}
return v1 == v2
}
func main() {
v1 := interface{}(func() {})
v2 := interface{}(func() {})
Equal(v1, v2)
}