diff options
| author | Ian Lance Taylor <iant@golang.org> | 2020-08-28 16:58:56 -0700 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2020-08-29 01:07:27 +0000 |
| commit | 9d511e4c6ea0413d2a2662562d0d41d8da93e6fc (patch) | |
| tree | 4c46c0a61970b4798ebbaeb830a63d2d869655ef | |
| parent | c480748687a2d694c8725fd6011ac0c9ddbecd92 (diff) | |
| download | go-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.md | 21 |
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 |
