From 85dcc34e0dad613f7f7d0915a52bdacedd570c3a Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Fri, 20 Nov 2015 07:00:09 -0800 Subject: [PATCH] doc: add FAQ entry about covariant result types Change-Id: If22b8f358e78deca31bd0b1a25e7966987853405 Reviewed-on: https://go-review.googlesource.com/17083 Reviewed-by: Rob Pike --- doc/go_faq.html | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/doc/go_faq.html b/doc/go_faq.html index 33636fca39..f198379fe5 100644 --- a/doc/go_faq.html +++ b/doc/go_faq.html @@ -860,6 +860,36 @@ value to hold the error and a type switch to discriminate cases. The syntax tree example is also doable, although not as elegantly.

+

+Why does Go not have covariant result types?

+ +

+Covariant result types would mean that an interface like + +

+type Copyable interface {
+	Copy() interface{}
+}
+
+ +would be satisfied by the method + +
+func (v Value) Copy() Value
+
+ +because Value implements the empty interface. +In Go method types must match exactly, so Value does not +implement Copyable. +Go separates the notion of what a +type does—its methods—from the type's implementation. +If two methods return different types, they are not doing the same thing. +Programmers who want covariant result types are often trying to +express a type heirarchy through interfaces. +In Go it's more natural to have a clean separation between interface +and implementation. +

+

Values