Debian Codegraph @

typeparams/example/interfaces/main.go

raw symbols SCIP
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 //go:build go1.18
6
7 package main
8
9 import (
10 "fmt"
11 "go/ast"
12 "go/parser"
13 "go/token"
14 "go/types"
15 "log"
16 )
17
18 const src = `
19 //!+input
20 package p
21
22 type Numeric interface{
23 ~int | ~float64 // etc...
24 }
25
26 func Square[N Numeric](n N) N {
27 return n*n
28 }
29
30 type Findable interface {
31 comparable
32 }
33
34 func Find[T Findable](s []T, v T) int {
35 for i, v2 := range s {
36 if v2 == v {
37 return i
38 }
39 }
40 return -1
41 }
42 //!-input
43 `
44
45 // !+printsyntax
46 func PrintNumericSyntax(fset *token.FileSet, file *ast.File) {
47 // node is the AST node corresponding to the declaration for "Numeric."
48 node := file.Scope.Lookup("Numeric").Decl.(*ast.TypeSpec)
49 // Find the embedded syntax node.
50 embedded := node.Type.(*ast.InterfaceType).Methods.List[0].Type
51 // Use go/ast's built-in Print function to inspect the parsed syntax.
52 ast.Print(fset, embedded)
53 }
54
55 //!-printsyntax
56
57 /*
58 //!+outputsyntax
59 0 *ast.BinaryExpr {
60 1 . X: *ast.UnaryExpr {
61 2 . . OpPos: p.go:6:2
62 3 . . Op: ~
63 4 . . X: *ast.Ident {
64 5 . . . NamePos: p.go:6:3
65 6 . . . Name: "int"
66 7 . . }
67 8 . }
68 9 . OpPos: p.go:6:7
69 10 . Op: |
70 11 . Y: *ast.UnaryExpr {
71 12 . . OpPos: p.go:6:9
72 13 . . Op: ~
73 14 . . X: *ast.Ident {
74 15 . . . NamePos: p.go:6:10
75 16 . . . Name: "float64"
76 17 . . }
77 18 . }
78 19 }
79 //!-outputsyntax
80 */
81
82 // !+printtypes
83 func PrintInterfaceTypes(fset *token.FileSet, file *ast.File) error {
84 conf := types.Config{}
85 pkg, err := conf.Check("hello", fset, []*ast.File{file}, nil)
86 if err != nil {
87 return err
88 }
89
90 PrintIface(pkg, "Numeric")
91 PrintIface(pkg, "Findable")
92
93 return nil
94 }
95
96 func PrintIface(pkg *types.Package, name string) {
97 obj := pkg.Scope().Lookup(name)
98 fmt.Println(obj)
99 iface := obj.Type().Underlying().(*types.Interface)
100 for i := 0; i < iface.NumEmbeddeds(); i++ {
101 fmt.Printf("\tembeded: %s\n", iface.EmbeddedType(i))
102 }
103 for i := 0; i < iface.NumMethods(); i++ {
104 fmt.Printf("\tembeded: %s\n", iface.EmbeddedType(i))
105 }
106 fmt.Printf("\tIsComparable(): %t\n", iface.IsComparable())
107 fmt.Printf("\tIsMethodSet(): %t\n", iface.IsMethodSet())
108 }
109
110 //!-printtypes
111
112 /*
113 //!+outputtypes
114 type hello.Numeric interface{~int|~float64}
115 embeded: ~int|~float64
116 IsComparable(): true
117 IsMethodSet(): false
118 type hello.Findable interface{comparable}
119 embeded: comparable
120 IsComparable(): true
121 IsMethodSet(): false
122 //!-outputtypes
123 */
124
125 func main() {
126 // Parse one file.
127 fset := token.NewFileSet()
128 f, err := parser.ParseFile(fset, "p.go", src, 0)
129 if err != nil {
130 log.Fatal(err)
131 }
132 fmt.Println("=== PrintNumericSyntax ==")
133 PrintNumericSyntax(fset, f)
134 fmt.Println("=== PrintInterfaceTypes ==")
135 if err := PrintInterfaceTypes(fset, f); err != nil {
136 log.Fatal(err)
137 }
138 }