| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"go/ast" |
| 6 |
"go/parser" |
| 7 |
"go/token" |
| 8 |
"go/types" |
| 9 |
"log" |
| 10 |
) |
| 11 |
|
| 12 |
const src = ` |
| 13 |
//!+input |
| 14 |
package p |
| 15 |
|
| 16 |
func Square[N ~int|~float64](n N) N { |
| 17 |
return n*n |
| 18 |
} |
| 19 |
//!-input |
| 20 |
` |
| 21 |
|
| 22 |
|
| 23 |
func ShowImplicit(pkg *types.Package) { |
| 24 |
Square := pkg.Scope().Lookup("Square").Type().(*types.Signature) |
| 25 |
N := Square.TypeParams().At(0) |
| 26 |
constraint := N.Constraint().(*types.Interface) |
| 27 |
fmt.Println(constraint) |
| 28 |
fmt.Println("IsImplicit:", constraint.IsImplicit()) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func main() { |
| 41 |
fset := token.NewFileSet() |
| 42 |
f, err := parser.ParseFile(fset, "p.go", src, 0) |
| 43 |
if err != nil { |
| 44 |
log.Fatal(err) |
| 45 |
} |
| 46 |
conf := types.Config{} |
| 47 |
pkg, err := conf.Check("typesets", fset, []*ast.File{f}, nil) |
| 48 |
if err != nil { |
| 49 |
log.Fatal(err) |
| 50 |
} |
| 51 |
ShowImplicit(pkg) |
| 52 |
} |