On Wed, Nov 30, 2016 at 1:12 PM, <groenendaa...@gmail.com> wrote: > > I had a question and when I looked through the FAQ I saw it: > https://golang.org/doc/faq#covariant_types. But could anyone give more > information specifically as to why Go does not have "covariant types"? What > is > the motivation behind that choice? If Go did support it, what kind of > problems > could crop up?
1) Covariant types are semantically complex. Right now whether a type implements an interface is simple. It must have a method with the same name as each interface method, and the types must match exactly. The normal definition of covariant types depends on a type hierarchy that does not exist in Go. So what are we really saying? A type implements an interface if it has a method with the same name as each interface method, and the types match exactly, or the type method's result parameters are assignable to the interface method's result parameters? Why restrict it to result parameters--shouldn't we also permit the interface method's input parameters to be assignable to the type method's input parameters? It is now much harder to understand whether a type implements an interface. 2) It's difficult to implement. The type method returns a value of one type, and the interface method returns a value of a completely different type. Somewhere there must be an assignment from one type to another. We know that the assignment will succeed, but it still must be implemented. There is no current place to implement that assignment. Currently interfaces are implemented by simply calling the type's method. There is no place to write code to convert the result. Consider in particular that you can use a type assertion to convert one interface type to another, and that may introduce a required conversion that was never seen by the compiler. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.