| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
// Test wrappers/interfaces for generic type embedding another generic type. |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
import "fmt" |
| 12 |
|
| 13 |
type A[T any] struct { |
| 14 |
B[T] |
| 15 |
} |
| 16 |
|
| 17 |
type B[T any] struct { |
| 18 |
val T |
| 19 |
} |
| 20 |
|
| 21 |
func (b *B[T]) get() T { |
| 22 |
return b.val |
| 23 |
} |
| 24 |
|
| 25 |
type getter[T any] interface { |
| 26 |
get() T |
| 27 |
} |
| 28 |
|
| 29 |
//go:noinline |
| 30 |
func doGet[T any](i getter[T]) T { |
| 31 |
return i.get() |
| 32 |
} |
| 33 |
|
| 34 |
//go:noline |
| 35 |
func doGet2[T any](i interface{}) T { |
| 36 |
i2 := i.(getter[T]) |
| 37 |
return i2.get() |
| 38 |
} |
| 39 |
|
| 40 |
func main() { |
| 41 |
a := A[int]{B: B[int]{3}} |
| 42 |
var i getter[int] = &a |
| 43 |
|
| 44 |
if got, want := doGet(i), 3; got != want { |
| 45 |
panic(fmt.Sprintf("got %v, want %v", got, want)) |
| 46 |
} |
| 47 |
|
| 48 |
as := A[string]{B: B[string]{"abc"}} |
| 49 |
if got, want := doGet2[string](&as), "abc"; got != want { |
| 50 |
panic(fmt.Sprintf("got %v, want %v", got, want)) |
| 51 |
} |
| 52 |
} |