mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[dev.typeparams] test/typeparam: gofmt -w
We don't usually reformat the test directory, but all of the files in test/typeparam are syntactically valid. I suspect the misformattings here are because developers aren't re-installing gofmt with -tags=typeparams, not intentionally exercising non-standard formatting. Change-Id: I3767d480434c19225568f3c7d656dc8589197183 Reviewed-on: https://go-review.googlesource.com/c/go/+/338093 Trust: Matthew Dempsky <mdempsky@google.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
473e493d18
commit
5355753009
59 changed files with 413 additions and 408 deletions
|
|
@ -61,7 +61,7 @@ type complexAbs[T Complex] T
|
||||||
func (a complexAbs[T]) Abs() complexAbs[T] {
|
func (a complexAbs[T]) Abs() complexAbs[T] {
|
||||||
r := float64(real(a))
|
r := float64(real(a))
|
||||||
i := float64(imag(a))
|
i := float64(imag(a))
|
||||||
d := math.Sqrt(r * r + i * i)
|
d := math.Sqrt(r*r + i*i)
|
||||||
return complexAbs[T](complex(d, 0))
|
return complexAbs[T](complex(d, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,10 +88,10 @@ func main() {
|
||||||
panic(fmt.Sprintf("got = %v, want = %v", got, want))
|
panic(fmt.Sprintf("got = %v, want = %v", got, want))
|
||||||
}
|
}
|
||||||
|
|
||||||
if got, want := complexAbsDifference(5.0 + 2.0i, 2.0 - 2.0i), 5+0i; got != want {
|
if got, want := complexAbsDifference(5.0+2.0i, 2.0-2.0i), 5+0i; got != want {
|
||||||
panic(fmt.Sprintf("got = %v, want = %v", got, want))
|
panic(fmt.Sprintf("got = %v, want = %v", got, want))
|
||||||
}
|
}
|
||||||
if got, want := complexAbsDifference(2.0 - 2.0i, 5.0 + 2.0i), 5+0i; got != want {
|
if got, want := complexAbsDifference(2.0-2.0i, 5.0+2.0i), 5+0i; got != want {
|
||||||
panic(fmt.Sprintf("got = %v, want = %v", got, want))
|
panic(fmt.Sprintf("got = %v, want = %v", got, want))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ type complexAbs[T Complex] T
|
||||||
func (a complexAbs[T]) Abs() complexAbs[T] {
|
func (a complexAbs[T]) Abs() complexAbs[T] {
|
||||||
r := float64(real(a))
|
r := float64(real(a))
|
||||||
i := float64(imag(a))
|
i := float64(imag(a))
|
||||||
d := math.Sqrt(r * r + i * i)
|
d := math.Sqrt(r*r + i*i)
|
||||||
return complexAbs[T](complex(d, 0))
|
return complexAbs[T](complex(d, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,9 @@ func _[T C5[X], X any](ch T) {
|
||||||
|
|
||||||
type M0 interface{ int }
|
type M0 interface{ int }
|
||||||
type M1 interface{ map[string]int }
|
type M1 interface{ map[string]int }
|
||||||
type M2 interface { map[string]int | map[string]float64 }
|
type M2 interface {
|
||||||
|
map[string]int | map[string]float64
|
||||||
|
}
|
||||||
type M3 interface{ map[string]int | map[rune]int }
|
type M3 interface{ map[string]int | map[rune]int }
|
||||||
type M4[K comparable, V any] interface{ map[K]V | map[rune]V }
|
type M4[K comparable, V any] interface{ map[K]V | map[rune]V }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -183,7 +183,7 @@ func _Ranger[Elem any]() (*_Sender[Elem], *_Receiver[Elem]) {
|
||||||
values: c,
|
values: c,
|
||||||
done: d,
|
done: d,
|
||||||
}
|
}
|
||||||
r := &_Receiver[Elem] {
|
r := &_Receiver[Elem]{
|
||||||
values: c,
|
values: c,
|
||||||
done: d,
|
done: d,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -176,7 +176,7 @@ func Ranger[Elem any]() (*Sender[Elem], *Receiver[Elem]) {
|
||||||
values: c,
|
values: c,
|
||||||
done: d,
|
done: d,
|
||||||
}
|
}
|
||||||
r := &Receiver[Elem] {
|
r := &Receiver[Elem]{
|
||||||
values: c,
|
values: c,
|
||||||
done: d,
|
done: d,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,6 @@ func main() {
|
||||||
}
|
}
|
||||||
gc4 := Combine2(g1, g3)
|
gc4 := Combine2(g1, g3)
|
||||||
if got, ok := gc4(); !ok || got.A != 3 || got.B != "y" {
|
if got, ok := gc4(); !ok || got.A != 3 || got.B != "y" {
|
||||||
panic (fmt.Sprintf("got %v, %v, wanted {3, y}, true", got, ok))
|
panic(fmt.Sprintf("got %v, %v, wanted {3, y}, true", got, ok))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ type List[a any] interface {
|
||||||
Match(casenil Function[Nil[a], any], casecons Function[Cons[a], any]) any
|
Match(casenil Function[Nil[a], any], casecons Function[Cons[a], any]) any
|
||||||
}
|
}
|
||||||
|
|
||||||
type Nil[a any] struct{
|
type Nil[a any] struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (xs Nil[a]) Match(casenil Function[Nil[a], any], casecons Function[Cons[a], any]) any {
|
func (xs Nil[a]) Match(casenil Function[Nil[a], any], casecons Function[Cons[a], any]) any {
|
||||||
|
|
@ -67,7 +67,7 @@ func (xs Cons[a]) Match(casenil Function[Nil[a], any], casecons Function[Cons[a]
|
||||||
return casecons.Apply(xs)
|
return casecons.Apply(xs)
|
||||||
}
|
}
|
||||||
|
|
||||||
type mapNil[a, b any] struct{
|
type mapNil[a, b any] struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m mapNil[a, b]) Apply(_ Nil[a]) any {
|
func (m mapNil[a, b]) Apply(_ Nil[a]) any {
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ func is7(x int) {
|
||||||
}
|
}
|
||||||
func is77(x, y int) {
|
func is77(x, y int) {
|
||||||
if x != 7 || y != 7 {
|
if x != 7 || y != 7 {
|
||||||
println(x,y)
|
println(x, y)
|
||||||
panic("assertion failed")
|
panic("assertion failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -63,7 +63,7 @@ func (x s[T]) g2() (T, T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func methodExpressions() {
|
func methodExpressions() {
|
||||||
x := s[int]{a:7}
|
x := s[int]{a: 7}
|
||||||
f0 := s[int].g0
|
f0 := s[int].g0
|
||||||
f0(x)
|
f0(x)
|
||||||
f1 := s[int].g1
|
f1 := s[int].g1
|
||||||
|
|
@ -73,7 +73,7 @@ func methodExpressions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func methodValues() {
|
func methodValues() {
|
||||||
x := s[int]{a:7}
|
x := s[int]{a: 7}
|
||||||
f0 := x.g0
|
f0 := x.g0
|
||||||
f0()
|
f0()
|
||||||
f1 := x.g1
|
f1 := x.g1
|
||||||
|
|
@ -82,20 +82,20 @@ func methodValues() {
|
||||||
is77(f2())
|
is77(f2())
|
||||||
}
|
}
|
||||||
|
|
||||||
var x interface{
|
var x interface {
|
||||||
g0()
|
g0()
|
||||||
g1()int
|
g1() int
|
||||||
g2()(int,int)
|
g2() (int, int)
|
||||||
} = s[int]{a:7}
|
} = s[int]{a: 7}
|
||||||
var y interface{} = s[int]{a:7}
|
var y interface{} = s[int]{a: 7}
|
||||||
|
|
||||||
func interfaceMethods() {
|
func interfaceMethods() {
|
||||||
x.g0()
|
x.g0()
|
||||||
is7(x.g1())
|
is7(x.g1())
|
||||||
is77(x.g2())
|
is77(x.g2())
|
||||||
y.(interface{g0()}).g0()
|
y.(interface{ g0() }).g0()
|
||||||
is7(y.(interface{g1()int}).g1())
|
is7(y.(interface{ g1() int }).g1())
|
||||||
is77(y.(interface{g2()(int,int)}).g2())
|
is77(y.(interface{ g2() (int, int) }).g2())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also check for instantiations outside functions.
|
// Also check for instantiations outside functions.
|
||||||
|
|
@ -107,7 +107,7 @@ var hh0 = s[int].g0
|
||||||
var hh1 = s[int].g1
|
var hh1 = s[int].g1
|
||||||
var hh2 = s[int].g2
|
var hh2 = s[int].g2
|
||||||
|
|
||||||
var xtop = s[int]{a:7}
|
var xtop = s[int]{a: 7}
|
||||||
var ii0 = x.g0
|
var ii0 = x.g0
|
||||||
var ii1 = x.g1
|
var ii1 = x.g1
|
||||||
var ii2 = x.g2
|
var ii2 = x.g2
|
||||||
|
|
@ -116,7 +116,7 @@ func globals() {
|
||||||
gg0(7)
|
gg0(7)
|
||||||
is7(gg1(7))
|
is7(gg1(7))
|
||||||
is77(gg2(7))
|
is77(gg2(7))
|
||||||
x := s[int]{a:7}
|
x := s[int]{a: 7}
|
||||||
hh0(x)
|
hh0(x)
|
||||||
is7(hh1(x))
|
is7(hh1(x))
|
||||||
is77(hh2(x))
|
is77(hh2(x))
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ func is7(x int) {
|
||||||
}
|
}
|
||||||
func is77(x, y int) {
|
func is77(x, y int) {
|
||||||
if x != 7 || y != 7 {
|
if x != 7 || y != 7 {
|
||||||
println(x,y)
|
println(x, y)
|
||||||
panic("assertion failed")
|
panic("assertion failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +70,7 @@ func (x s[T]) g2() (T, T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func methodExpressions() {
|
func methodExpressions() {
|
||||||
x := s[int]{a:7}
|
x := s[int]{a: 7}
|
||||||
f0 := s[int].g0
|
f0 := s[int].g0
|
||||||
f0(x)
|
f0(x)
|
||||||
f0p := (*s[int]).g0
|
f0p := (*s[int]).g0
|
||||||
|
|
@ -106,7 +106,7 @@ func genMethodExpressions[T comparable](want T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func methodValues() {
|
func methodValues() {
|
||||||
x := s[int]{a:7}
|
x := s[int]{a: 7}
|
||||||
f0 := x.g0
|
f0 := x.g0
|
||||||
f0()
|
f0()
|
||||||
f1 := x.g1
|
f1 := x.g1
|
||||||
|
|
@ -129,20 +129,20 @@ func genMethodValues[T comparable](want T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var x interface{
|
var x interface {
|
||||||
g0()
|
g0()
|
||||||
g1()int
|
g1() int
|
||||||
g2()(int,int)
|
g2() (int, int)
|
||||||
} = s[int]{a:7}
|
} = s[int]{a: 7}
|
||||||
var y interface{} = s[int]{a:7}
|
var y interface{} = s[int]{a: 7}
|
||||||
|
|
||||||
func interfaceMethods() {
|
func interfaceMethods() {
|
||||||
x.g0()
|
x.g0()
|
||||||
is7(x.g1())
|
is7(x.g1())
|
||||||
is77(x.g2())
|
is77(x.g2())
|
||||||
y.(interface{g0()}).g0()
|
y.(interface{ g0() }).g0()
|
||||||
is7(y.(interface{g1()int}).g1())
|
is7(y.(interface{ g1() int }).g1())
|
||||||
is77(y.(interface{g2()(int,int)}).g2())
|
is77(y.(interface{ g2() (int, int) }).g2())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also check for instantiations outside functions.
|
// Also check for instantiations outside functions.
|
||||||
|
|
@ -154,7 +154,7 @@ var hh0 = s[int].g0
|
||||||
var hh1 = s[int].g1
|
var hh1 = s[int].g1
|
||||||
var hh2 = s[int].g2
|
var hh2 = s[int].g2
|
||||||
|
|
||||||
var xtop = s[int]{a:7}
|
var xtop = s[int]{a: 7}
|
||||||
var ii0 = x.g0
|
var ii0 = x.g0
|
||||||
var ii1 = x.g1
|
var ii1 = x.g1
|
||||||
var ii2 = x.g2
|
var ii2 = x.g2
|
||||||
|
|
@ -163,7 +163,7 @@ func globals() {
|
||||||
gg0(7)
|
gg0(7)
|
||||||
is7(gg1(7))
|
is7(gg1(7))
|
||||||
is77(gg2(7))
|
is77(gg2(7))
|
||||||
x := s[int]{a:7}
|
x := s[int]{a: 7}
|
||||||
hh0(x)
|
hh0(x)
|
||||||
is7(hh1(x))
|
is7(hh1(x))
|
||||||
is77(hh2(x))
|
is77(hh2(x))
|
||||||
|
|
@ -172,7 +172,6 @@ func globals() {
|
||||||
is77(ii2())
|
is77(ii2())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func recursive() {
|
func recursive() {
|
||||||
if got, want := recur1[int](5), 110; got != want {
|
if got, want := recur1[int](5), 110; got != want {
|
||||||
panic(fmt.Sprintf("recur1[int](5) = %d, want = %d", got, want))
|
panic(fmt.Sprintf("recur1[int](5) = %d, want = %d", got, want))
|
||||||
|
|
@ -187,14 +186,14 @@ func recur1[T Integer](n T) T {
|
||||||
if n == 0 || n == 1 {
|
if n == 0 || n == 1 {
|
||||||
return T(1)
|
return T(1)
|
||||||
} else {
|
} else {
|
||||||
return n * recur2(n - 1)
|
return n * recur2(n-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func recur2[T Integer](n T) T {
|
func recur2[T Integer](n T) T {
|
||||||
list := make([]T, n)
|
list := make([]T, n)
|
||||||
for i, _ := range list {
|
for i, _ := range list {
|
||||||
list[i] = T(i+1)
|
list[i] = T(i + 1)
|
||||||
}
|
}
|
||||||
var sum T
|
var sum T
|
||||||
for _, elt := range list {
|
for _, elt := range list {
|
||||||
|
|
|
||||||
|
|
@ -36,20 +36,20 @@ func g2[T I](x I) (T, bool) {
|
||||||
return t, ok
|
return t, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func h[T any](x interface{}) struct{a, b T} {
|
func h[T any](x interface{}) struct{ a, b T } {
|
||||||
return x.(struct{a, b T})
|
return x.(struct{ a, b T })
|
||||||
}
|
}
|
||||||
|
|
||||||
func k[T any](x interface{}) interface { bar() T } {
|
func k[T any](x interface{}) interface{ bar() T } {
|
||||||
return x.(interface{bar() T })
|
return x.(interface{ bar() T })
|
||||||
}
|
}
|
||||||
|
|
||||||
type mybar int
|
type mybar int
|
||||||
|
|
||||||
func (x mybar) bar() int {
|
func (x mybar) bar() int {
|
||||||
return int(x)
|
return int(x)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var i interface{} = int(3)
|
var i interface{} = int(3)
|
||||||
var j I = myint(3)
|
var j I = myint(3)
|
||||||
|
|
@ -66,7 +66,7 @@ func main() {
|
||||||
println(g2[myint](j))
|
println(g2[myint](j))
|
||||||
println(g2[myint](y))
|
println(g2[myint](y))
|
||||||
|
|
||||||
println(h[int](struct{a, b int}{3, 5}).a)
|
println(h[int](struct{ a, b int }{3, 5}).a)
|
||||||
|
|
||||||
println(k[int](mybar(3)).bar())
|
println(k[int](mybar(3)).bar())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ func (x myint) foo() {
|
||||||
|
|
||||||
func k[T comparable](t T, i interface{}) bool {
|
func k[T comparable](t T, i interface{}) bool {
|
||||||
// Compare derived type value to interface.
|
// Compare derived type value to interface.
|
||||||
return struct{a, b T}{t, t} == i
|
return struct{ a, b T }{t, t} == i
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -51,15 +51,15 @@ func main() {
|
||||||
assert(h(myint(3), myint(3)))
|
assert(h(myint(3), myint(3)))
|
||||||
assert(!h(myint(3), myint(5)))
|
assert(!h(myint(3), myint(5)))
|
||||||
|
|
||||||
type S struct { a, b float64 }
|
type S struct{ a, b float64 }
|
||||||
|
|
||||||
assert(f(S{3,5}, S{3,5}))
|
assert(f(S{3, 5}, S{3, 5}))
|
||||||
assert(!f(S{3,5}, S{4,6}))
|
assert(!f(S{3, 5}, S{4, 6}))
|
||||||
assert(g(S{3,5}, S{3,5}))
|
assert(g(S{3, 5}, S{3, 5}))
|
||||||
assert(!g(S{3,5}, S{4,6}))
|
assert(!g(S{3, 5}, S{4, 6}))
|
||||||
|
|
||||||
assert(k(3, struct{a, b int}{3, 3}))
|
assert(k(3, struct{ a, b int }{3, 3}))
|
||||||
assert(!k(3, struct{a, b int}{3, 4}))
|
assert(!k(3, struct{ a, b int }{3, 4}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func assert(b bool) {
|
func assert(b bool) {
|
||||||
|
|
@ -67,5 +67,3 @@ func assert(b bool) {
|
||||||
panic("assertion failed")
|
panic("assertion failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,11 @@ package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func fact[T interface { ~int | ~int64 | ~float64 }](n T) T {
|
func fact[T interface{ ~int | ~int64 | ~float64 }](n T) T {
|
||||||
if n == 1 {
|
if n == 1 {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return n * fact(n - 1)
|
return n * fact(n-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
package a
|
package a
|
||||||
|
|
||||||
func Fact[T interface { int | int64 | float64 }](n T) T {
|
func Fact[T interface{ int | int64 | float64 }](n T) T {
|
||||||
if n == 1 {
|
if n == 1 {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return n * Fact(n - 1)
|
return n * Fact(n-1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -225,7 +225,6 @@ func TestShortestPath() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
TestShortestPath()
|
TestShortestPath()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,11 +37,11 @@ type C interface {
|
||||||
type myInt int
|
type myInt int
|
||||||
|
|
||||||
func (x myInt) foo() int {
|
func (x myInt) foo() int {
|
||||||
return int(x+1)
|
return int(x + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func h[T C](x T) interface{foo() int} {
|
func h[T C](x T) interface{ foo() int } {
|
||||||
var i interface{foo()int} = x
|
var i interface{ foo() int } = x
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
func i[T C](x T) C {
|
func i[T C](x T) C {
|
||||||
|
|
|
||||||
|
|
@ -79,4 +79,3 @@ func main() {
|
||||||
panic(fmt.Sprintf("got %d, want %d", got, want))
|
panic(fmt.Sprintf("got %d, want %d", got, want))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,11 @@ func F() {
|
||||||
|
|
||||||
// Testing the various combinations of method expressions.
|
// Testing the various combinations of method expressions.
|
||||||
type S1 struct{}
|
type S1 struct{}
|
||||||
|
|
||||||
func (*S1) M() {}
|
func (*S1) M() {}
|
||||||
|
|
||||||
type S2 struct{}
|
type S2 struct{}
|
||||||
|
|
||||||
func (S2) M() {}
|
func (S2) M() {}
|
||||||
|
|
||||||
func _F1[T interface{ M() }](t T) {
|
func _F1[T interface{ M() }](t T) {
|
||||||
|
|
|
||||||
|
|
@ -8,14 +8,14 @@
|
||||||
|
|
||||||
package p
|
package p
|
||||||
|
|
||||||
func _[T interface{~map[string]int}](x T) {
|
func _[T interface{ ~map[string]int }](x T) {
|
||||||
_ = x == nil
|
_ = x == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// simplified test case from issue
|
// simplified test case from issue
|
||||||
|
|
||||||
type PathParamsConstraint interface {
|
type PathParamsConstraint interface {
|
||||||
~map[string]string | ~[]struct{key, value string}
|
~map[string]string | ~[]struct{ key, value string }
|
||||||
}
|
}
|
||||||
|
|
||||||
type PathParams[T PathParamsConstraint] struct {
|
type PathParams[T PathParamsConstraint] struct {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type A1[T any] struct{
|
type A1[T any] struct {
|
||||||
val T
|
val T
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,12 @@ import (
|
||||||
type s[T any] struct {
|
type s[T any] struct {
|
||||||
a T
|
a T
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x s[T]) f() T {
|
func (x s[T]) f() T {
|
||||||
return x.a
|
return x.a
|
||||||
}
|
}
|
||||||
func main() {
|
func main() {
|
||||||
x := s[int]{a:7}
|
x := s[int]{a: 7}
|
||||||
f := x.f
|
f := x.f
|
||||||
if got, want := f(), 7; got != want {
|
if got, want := f(), 7; got != want {
|
||||||
panic(fmt.Sprintf("got %d, want %d", got, want))
|
panic(fmt.Sprintf("got %d, want %d", got, want))
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ func foo[T any](d T) {
|
||||||
switch v := interface{}(d).(type) {
|
switch v := interface{}(d).(type) {
|
||||||
case string:
|
case string:
|
||||||
if v != "x" {
|
if v != "x" {
|
||||||
panic("unexpected v: "+v)
|
panic("unexpected v: " + v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Numeric interface {
|
type Numeric interface {
|
||||||
int32|int64|float64|complex64
|
int32 | int64 | float64 | complex64
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:noline
|
//go:noline
|
||||||
|
|
|
||||||
|
|
@ -594,7 +594,6 @@ func TestTransform() {
|
||||||
checkList(l2, []interface{}{"1", "2"})
|
checkList(l2, []interface{}{"1", "2"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
TestList()
|
TestList()
|
||||||
TestExtending()
|
TestExtending()
|
||||||
|
|
@ -607,4 +606,3 @@ func main() {
|
||||||
TestInsertAfterUnknownMark()
|
TestInsertAfterUnknownMark()
|
||||||
TestTransform()
|
TestTransform()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -301,7 +301,6 @@ func TestTransform() {
|
||||||
checkList(l2, []interface{}{"1", "2"})
|
checkList(l2, []interface{}{"1", "2"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
TestList()
|
TestList()
|
||||||
TestExtending()
|
TestExtending()
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
package a
|
package a
|
||||||
|
|
||||||
type X int
|
type X int
|
||||||
|
|
||||||
func (x X) M() X { return x }
|
func (x X) M() X { return x }
|
||||||
|
|
||||||
func F[T interface{ M() U }, U interface{ M() T }]() {}
|
func F[T interface{ M() U }, U interface{ M() T }]() {}
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ func testOrdered[Elem Ordered](name string, s []Elem, sorter func([]Elem)) bool
|
||||||
}
|
}
|
||||||
for i := len(s1) - 1; i > 0; i-- {
|
for i := len(s1) - 1; i > 0; i-- {
|
||||||
if s1[i] < s1[i-1] {
|
if s1[i] < s1[i-1] {
|
||||||
fmt.Printf("%s: element %d (%v) < element %d (%v)", name, i, s1[i], i - 1, s1[i - 1])
|
fmt.Printf("%s: element %d (%v) < element %d (%v)", name, i, s1[i], i-1, s1[i-1])
|
||||||
ok = false
|
ok = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -230,7 +230,7 @@ func _Ranger[Elem any]() (*_Sender[Elem], *_Receiver[Elem]) {
|
||||||
values: c,
|
values: c,
|
||||||
done: d,
|
done: d,
|
||||||
}
|
}
|
||||||
r := &_Receiver[Elem] {
|
r := &_Receiver[Elem]{
|
||||||
values: c,
|
values: c,
|
||||||
done: d,
|
done: d,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -170,7 +170,7 @@ func Ranger[Elem any]() (*Sender[Elem], *Receiver[Elem]) {
|
||||||
values: c,
|
values: c,
|
||||||
done: d,
|
done: d,
|
||||||
}
|
}
|
||||||
r := &Receiver[Elem] {
|
r := &Receiver[Elem]{
|
||||||
values: c,
|
values: c,
|
||||||
done: d,
|
done: d,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ func TestMap() {
|
||||||
panic(fmt.Sprintf("unexpectedly found %q in empty map", []byte("a")))
|
panic(fmt.Sprintf("unexpectedly found %q in empty map", []byte("a")))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range []int{ 'a', 'c', 'b' } {
|
for _, c := range []int{'a', 'c', 'b'} {
|
||||||
if !m.Insert([]byte(string(c)), c) {
|
if !m.Insert([]byte(string(c)), c) {
|
||||||
panic(fmt.Sprintf("key %q unexpectedly already present", []byte(string(c))))
|
panic(fmt.Sprintf("key %q unexpectedly already present", []byte(string(c))))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,10 @@ func main() {
|
||||||
panic(fmt.Sprintf("unexpected f2 size == %d, want %d", got, want))
|
panic(fmt.Sprintf("unexpected f2 size == %d, want %d", got, want))
|
||||||
}
|
}
|
||||||
|
|
||||||
type mypair struct { f1 int32; f2 int64 }
|
type mypair struct {
|
||||||
|
f1 int32
|
||||||
|
f2 int64
|
||||||
|
}
|
||||||
mp := mypair(p)
|
mp := mypair(p)
|
||||||
if mp.f1 != 1 || mp.f2 != 2 {
|
if mp.f1 != 1 || mp.f2 != 2 {
|
||||||
panic(fmt.Sprintf("mp == %#v, want %#v", mp, mypair{1, 2}))
|
panic(fmt.Sprintf("mp == %#v, want %#v", mp, mypair{1, 2}))
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,10 @@ func main() {
|
||||||
panic(fmt.Sprintf("unexpected f2 size == %d, want %d", got, want))
|
panic(fmt.Sprintf("unexpected f2 size == %d, want %d", got, want))
|
||||||
}
|
}
|
||||||
|
|
||||||
type mypair struct { Field1 int32; Field2 int64 }
|
type mypair struct {
|
||||||
|
Field1 int32
|
||||||
|
Field2 int64
|
||||||
|
}
|
||||||
mp := mypair(p)
|
mp := mypair(p)
|
||||||
if mp.Field1 != 1 || mp.Field2 != 2 {
|
if mp.Field1 != 1 || mp.Field2 != 2 {
|
||||||
panic(fmt.Sprintf("mp == %#v, want %#v", mp, mypair{1, 2}))
|
panic(fmt.Sprintf("mp == %#v, want %#v", mp, mypair{1, 2}))
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,6 @@ func fromStrings1a[T any, PT Setter[T]](s []string) []PT {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Takes one type parameter and a set function
|
// Takes one type parameter and a set function
|
||||||
func fromStrings2[T any](s []string, set func(*T, string)) []T {
|
func fromStrings2[T any](s []string, set func(*T, string)) []T {
|
||||||
results := make([]T, len(s))
|
results := make([]T, len(s))
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ func Append[T any](s []T, t ...T) []T {
|
||||||
if tot <= cap(s) {
|
if tot <= cap(s) {
|
||||||
s = s[:tot]
|
s = s[:tot]
|
||||||
} else {
|
} else {
|
||||||
news := make([]T, tot, tot + tot/2)
|
news := make([]T, tot, tot+tot/2)
|
||||||
Copy(news, s)
|
Copy(news, s)
|
||||||
s = news
|
s = news
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ func TestEqual() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func offByOne[Elem Integer](a, b Elem) bool {
|
func offByOne[Elem Integer](a, b Elem) bool {
|
||||||
return a == b + 1 || a == b - 1
|
return a == b+1 || a == b-1
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEqualFn() {
|
func TestEqualFn() {
|
||||||
|
|
@ -92,12 +92,12 @@ func TestMap() {
|
||||||
|
|
||||||
func TestReduce() {
|
func TestReduce() {
|
||||||
s1 := []int{1, 2, 3}
|
s1 := []int{1, 2, 3}
|
||||||
r := a.Reduce(s1, 0, func(f float64, i int) float64 { return float64(i) * 2.5 + f })
|
r := a.Reduce(s1, 0, func(f float64, i int) float64 { return float64(i)*2.5 + f })
|
||||||
if want := 15.0; r != want {
|
if want := 15.0; r != want {
|
||||||
panic(fmt.Sprintf("a.Reduce(%v, 0, ...) = %v, want %v", s1, r, want))
|
panic(fmt.Sprintf("a.Reduce(%v, 0, ...) = %v, want %v", s1, r, want))
|
||||||
}
|
}
|
||||||
|
|
||||||
if got := a.Reduce(nil, 0, func(i, j int) int { return i + j}); got != 0 {
|
if got := a.Reduce(nil, 0, func(i, j int) int { return i + j }); got != 0 {
|
||||||
panic(fmt.Sprintf("a.Reduce(nil, 0, add) = %v, want 0", got))
|
panic(fmt.Sprintf("a.Reduce(nil, 0, add) = %v, want 0", got))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ func _Append[T any](s []T, t ...T) []T {
|
||||||
if tot <= cap(s) {
|
if tot <= cap(s) {
|
||||||
s = s[:tot]
|
s = s[:tot]
|
||||||
} else {
|
} else {
|
||||||
news := make([]T, tot, tot + tot/2)
|
news := make([]T, tot, tot+tot/2)
|
||||||
_Copy(news, s)
|
_Copy(news, s)
|
||||||
s = news
|
s = news
|
||||||
}
|
}
|
||||||
|
|
@ -186,7 +186,7 @@ func TestEqual() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func offByOne[Elem Integer](a, b Elem) bool {
|
func offByOne[Elem Integer](a, b Elem) bool {
|
||||||
return a == b + 1 || a == b - 1
|
return a == b+1 || a == b-1
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEqualFn() {
|
func TestEqualFn() {
|
||||||
|
|
@ -231,12 +231,12 @@ func TestMap() {
|
||||||
|
|
||||||
func TestReduce() {
|
func TestReduce() {
|
||||||
s1 := []int{1, 2, 3}
|
s1 := []int{1, 2, 3}
|
||||||
r := _Reduce(s1, 0, func(f float64, i int) float64 { return float64(i) * 2.5 + f })
|
r := _Reduce(s1, 0, func(f float64, i int) float64 { return float64(i)*2.5 + f })
|
||||||
if want := 15.0; r != want {
|
if want := 15.0; r != want {
|
||||||
panic(fmt.Sprintf("_Reduce(%v, 0, ...) = %v, want %v", s1, r, want))
|
panic(fmt.Sprintf("_Reduce(%v, 0, ...) = %v, want %v", s1, r, want))
|
||||||
}
|
}
|
||||||
|
|
||||||
if got := _Reduce(nil, 0, func(i, j int) int { return i + j}); got != 0 {
|
if got := _Reduce(nil, 0, func(i, j int) int { return i + j }); got != 0 {
|
||||||
panic(fmt.Sprintf("_Reduce(nil, 0, add) = %v, want 0", got))
|
panic(fmt.Sprintf("_Reduce(nil, 0, add) = %v, want 0", got))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ func (a myint) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
v := StringableList[myint]{ myint(1), myint(2) }
|
v := StringableList[myint]{myint(1), myint(2)}
|
||||||
|
|
||||||
if got, want := v.String(), "1, 2"; got != want {
|
if got, want := v.String(), "1, 2"; got != want {
|
||||||
panic(fmt.Sprintf("got %s, want %s", got, want))
|
panic(fmt.Sprintf("got %s, want %s", got, want))
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ func (v *value[T]) get(def T) T {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var s value[string]
|
var s value[string]
|
||||||
if got, want := s.get("ab"), ""; got != want {
|
if got, want := s.get("ab"), ""; got != want {
|
||||||
|
|
|
||||||
|
|
@ -40,11 +40,11 @@ func main() {
|
||||||
|
|
||||||
fwant := vec2[0] + vec2[1]
|
fwant := vec2[0] + vec2[1]
|
||||||
fgot := Sum[float64](vec2)
|
fgot := Sum[float64](vec2)
|
||||||
if Abs(fgot - fwant) > 1e-10 {
|
if Abs(fgot-fwant) > 1e-10 {
|
||||||
panic(fmt.Sprintf("got %f, want %f", fgot, fwant))
|
panic(fmt.Sprintf("got %f, want %f", fgot, fwant))
|
||||||
}
|
}
|
||||||
fgot = Sum(vec2)
|
fgot = Sum(vec2)
|
||||||
if Abs(fgot - fwant) > 1e-10 {
|
if Abs(fgot-fwant) > 1e-10 {
|
||||||
panic(fmt.Sprintf("got %f, want %f", fgot, fwant))
|
panic(fmt.Sprintf("got %f, want %f", fgot, fwant))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,13 @@ package tparam1
|
||||||
// in a type parameter list.
|
// in a type parameter list.
|
||||||
var _ any // ERROR "cannot use any outside constraint position"
|
var _ any // ERROR "cannot use any outside constraint position"
|
||||||
func _(_ any) // ERROR "cannot use any outside constraint position"
|
func _(_ any) // ERROR "cannot use any outside constraint position"
|
||||||
type _[_ any /* ok here */ ] struct{}
|
type _[_ any /* ok here */] struct{}
|
||||||
|
|
||||||
const N = 10
|
const N = 10
|
||||||
|
|
||||||
type (
|
type (
|
||||||
_[] struct{} // slice
|
_ []struct{} // slice
|
||||||
_[N] struct{} // array
|
_ [N]struct{} // array
|
||||||
_[T any] struct{}
|
_[T any] struct{}
|
||||||
_[T, T any] struct{} // ERROR "T redeclared"
|
_[T, T any] struct{} // ERROR "T redeclared"
|
||||||
_[T1, T2 any, T3 any] struct{}
|
_[T1, T2 any, T3 any] struct{}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ package p
|
||||||
|
|
||||||
// Assignability of an unnamed pointer type to a type parameter that
|
// Assignability of an unnamed pointer type to a type parameter that
|
||||||
// has a matching underlying type.
|
// has a matching underlying type.
|
||||||
func _[T interface{}, PT interface{type *T}] (x T) PT {
|
func _[T interface{}, PT interface{ type *T }](x T) PT {
|
||||||
return &x
|
return &x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ func at[T interface{ type []E }, E any](x T, i int) E {
|
||||||
// A generic type inside a function acts like a named type. Its underlying
|
// A generic type inside a function acts like a named type. Its underlying
|
||||||
// type is itself, its "operational type" is defined by the type list in
|
// type is itself, its "operational type" is defined by the type list in
|
||||||
// the tybe bound, if any.
|
// the tybe bound, if any.
|
||||||
func _[T interface{type int}](x T) {
|
func _[T interface{ type int }](x T) {
|
||||||
type myint int
|
type myint int
|
||||||
var _ int = int(x)
|
var _ int = int(x)
|
||||||
var _ T = 42
|
var _ T = 42
|
||||||
|
|
@ -30,7 +30,7 @@ func _[T interface{type int}](x T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Indexing a generic type which has a structural contraints to be an array.
|
// Indexing a generic type which has a structural contraints to be an array.
|
||||||
func _[T interface { type [10]int }](x T) {
|
func _[T interface{ type [10]int }](x T) {
|
||||||
_ = x[9] // ok
|
_ = x[9] // ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,7 +44,7 @@ func _[T interface{ type *int }](p T) int {
|
||||||
func _[T interface{ type chan int }](ch T) int {
|
func _[T interface{ type chan int }](ch T) int {
|
||||||
// This would deadlock if executed (but ok for a compile test)
|
// This would deadlock if executed (but ok for a compile test)
|
||||||
ch <- 0
|
ch <- 0
|
||||||
return <- ch
|
return <-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calling of a generic type which has a structural constraint to be a function.
|
// Calling of a generic type which has a structural constraint to be a function.
|
||||||
|
|
@ -59,11 +59,10 @@ func _[T interface{ type func(string) int }](f T) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map access of a generic type which has a structural constraint to be a map.
|
// Map access of a generic type which has a structural constraint to be a map.
|
||||||
func _[V any, T interface { type map[string]V }](p T) V {
|
func _[V any, T interface{ type map[string]V }](p T) V {
|
||||||
return p["test"]
|
return p["test"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Testing partial and full type inference, including the case where the types can
|
// Testing partial and full type inference, including the case where the types can
|
||||||
// be inferred without needing the types of the function arguments.
|
// be inferred without needing the types of the function arguments.
|
||||||
|
|
||||||
|
|
@ -86,7 +85,7 @@ func _() {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func f2[A any, B interface{type []A}](_ A, _ B)
|
func f2[A any, B interface{ type []A }](_ A, _ B)
|
||||||
func _() {
|
func _() {
|
||||||
f := f2[byte]
|
f := f2[byte]
|
||||||
f(byte(0), []byte{})
|
f(byte(0), []byte{})
|
||||||
|
|
@ -106,7 +105,7 @@ func _() {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func f4[A any, B interface{type []C}, C interface{type *A}](_ A, _ B, c C)
|
func f4[A any, B interface{ type []C }, C interface{ type *A }](_ A, _ B, c C)
|
||||||
func _() {
|
func _() {
|
||||||
f := f4[int]
|
f := f4[int]
|
||||||
var x int
|
var x int
|
||||||
|
|
@ -114,15 +113,20 @@ func _() {
|
||||||
f4(x, []*int{}, &x)
|
f4(x, []*int{}, &x)
|
||||||
}
|
}
|
||||||
|
|
||||||
func f5[A interface{type struct{b B; c C}}, B any, C interface{type *B}](x B) A
|
func f5[A interface {
|
||||||
|
type struct {
|
||||||
|
b B
|
||||||
|
c C
|
||||||
|
}
|
||||||
|
}, B any, C interface{ type *B }](x B) A
|
||||||
func _() {
|
func _() {
|
||||||
x := f5(1.2)
|
x := f5(1.2)
|
||||||
var _ float64 = x.b
|
var _ float64 = x.b
|
||||||
var _ float64 = *x.c
|
var _ float64 = *x.c
|
||||||
}
|
}
|
||||||
|
|
||||||
func f6[A any, B interface{type struct{f []A}}](B) A
|
func f6[A any, B interface{ type struct{ f []A } }](B) A
|
||||||
func _() {
|
func _() {
|
||||||
x := f6(struct{f []string}{})
|
x := f6(struct{ f []string }{})
|
||||||
var _ string = x
|
var _ string = x
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,4 +53,3 @@ func main() {
|
||||||
panic(fmt.Sprintf("Get() == %d, want %d", got, want))
|
panic(fmt.Sprintf("Get() == %d, want %d", got, want))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue