Can someone please point out why the Android
type here is illegal to use where it actually satisfy the comparable
interface and it implements the Human
interface.
package main
import "fmt"
func main() {
DoSomething(Person{})
// DoSomething(OddPerson{}) // Illegal since OddPerson has an underlying type map which is not comparable
// DoSomething(Dog{}) // Illegal since Dog does not implement Human
// DoSomething(Android{}) // Illegal, why?
a1 := Android(1)
a2 := Android(1)
fmt.Println(a1 == a2) // Android is comparable here
}
func DoSomething[T Organism](o T) {
o.Talk()
}
type Organism interface {
comparable
Human
}
type Human interface {
Talk() string
}
type Person struct{}
func (p Person) Talk() string {
return "I'm a person"
}
type OddPerson map[string]any
func (p OddPerson) Talk() string {
return "I'm a fake person"
}
type Dog struct{}
func (d Dog) Bark() string {
return "Woof!"
}
type Android int // Underlying type int for the sake of proving the Human interface
func (a Android) Talk() string {
return "Hello"
}