| 1 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package p |
| 6 |
|
| 7 |
import ( |
| 8 |
"reflect" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
type T1 int |
| 13 |
type T2 int |
| 14 |
|
| 15 |
func f[P T1 | T2, _ []P]() {} |
| 16 |
|
| 17 |
var _ = f[T1] |
| 18 |
|
| 19 |
// test case from issue |
| 20 |
|
| 21 |
type BaseT interface { |
| 22 |
Type1 | Type2 |
| 23 |
} |
| 24 |
type BaseType int |
| 25 |
type Type1 BaseType |
| 26 |
type Type2 BaseType // float64 |
| 27 |
|
| 28 |
type ValueT[T BaseT] struct { |
| 29 |
A1 T |
| 30 |
} |
| 31 |
|
| 32 |
func NewType1() *ValueT[Type1] { |
| 33 |
r := NewT[Type1]() |
| 34 |
return r |
| 35 |
} |
| 36 |
func NewType2() *ValueT[Type2] { |
| 37 |
r := NewT[Type2]() |
| 38 |
return r |
| 39 |
} |
| 40 |
|
| 41 |
func NewT[TBase BaseT, TVal ValueT[TBase]]() *TVal { |
| 42 |
ret := TVal{} |
| 43 |
return &ret |
| 44 |
} |
| 45 |
func TestGoType(t *testing.T) { |
| 46 |
r1 := NewType1() |
| 47 |
r2 := NewType2() |
| 48 |
t.Log(r1, r2) |
| 49 |
t.Log(reflect.TypeOf(r1), reflect.TypeOf(r2)) |
| 50 |
fooT1(r1.A1) |
| 51 |
fooT2(r2.A1) |
| 52 |
} |
| 53 |
|
| 54 |
func fooT1(t1 Type1) { |
| 55 |
|
| 56 |
} |
| 57 |
func fooT2(t2 Type2) { |
| 58 |
|
| 59 |
} |