如何确定泛型类型在运行时是否“可比较”?

如何确定泛型类型在运行时是否“可比较”?

问题内容

我想编写一个通用的 equals 方法,其工作原理如下:

func equals[T any](a, b T) bool { if hasEqualsMethod(T) { return a.Equals(b) else if isComparable(T) { return a == b } panic("type cannot be compared") } 登录后复制