aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2020-08-28 16:58:56 -0700
committerIan Lance Taylor <iant@golang.org>2020-08-29 01:07:27 +0000
commit9d511e4c6ea0413d2a2662562d0d41d8da93e6fc (patch)
tree4c46c0a61970b4798ebbaeb830a63d2d869655ef
parentc480748687a2d694c8725fd6011ac0c9ddbecd92 (diff)
downloadgo-x-proposal-9d511e4c6ea0413d2a2662562d0d41d8da93e6fc.tar.xz
design: type parameters: promote embedded methods from constraint
Change-Id: If4be0f74932c1ec504833549fb01a047dcf8335f Reviewed-on: https://go-review.googlesource.com/c/proposal/+/251444 Reviewed-by: Robert Griesemer <gri@golang.org>
-rw-r--r--design/go2draft-type-parameters.md21
1 files changed, 21 insertions, 0 deletions
diff --git a/design/go2draft-type-parameters.md b/design/go2draft-type-parameters.md
index 3dcb07b..54b1604 100644
--- a/design/go2draft-type-parameters.md
+++ b/design/go2draft-type-parameters.md
@@ -3710,6 +3710,27 @@ func (l *Lockable[T]) Set(v T) {
}
```
+### Embedded type parameter methods
+
+When a generic type is a struct, and the type parameter is embedded as
+a field in the struct, any methods of the type parameter's constraint
+are promoted to be methods of the struct.
+
+```Go
+// NamedInt is an int with a name. The name can be any type with
+// a String method.
+type NamedInt[Name fmt.Stringer] struct {
+ Name
+ val int
+}
+
+// Name returns the name of a NamedInt.
+func (ni NamedInt[Name]) Name() string {
+ // The String method is promoted from the embedded Name.
+ return ni.String()
+}
+```
+
### Embedded instantiated type
When embedding an instantiated type, the name of the field is the name