mirror of
https://github.com/golang/go.git
synced 2024-09-22 02:48:50 +00:00
40e561d933
First baby step to sharing the underlying implementation among several types. Change-Id: I6a156176d2b7f0131a87285a03b881ce380c26ed Reviewed-on: https://go-review.googlesource.com/c/go/+/338610 Trust: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dan Scales <danscales@google.com>
34 lines
533 B
Go
34 lines
533 B
Go
// run -gcflags=-G=3
|
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
type I interface {
|
|
foo() int
|
|
}
|
|
|
|
// There should be a single instantiation of f in this program.
|
|
func f[T I](x T) int {
|
|
return x.foo()
|
|
}
|
|
|
|
type squarer int
|
|
|
|
func (x squarer) foo() int {
|
|
return int(x*x)
|
|
}
|
|
|
|
type doubler int
|
|
|
|
func (x doubler) foo() int {
|
|
return int(2*x)
|
|
}
|
|
|
|
func main() {
|
|
println(f(squarer(5)))
|
|
println(f(doubler(5)))
|
|
}
|