Debian Codegraph @

typeparams/example/typesets/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 package main
6
7 import (
8 "fmt"
9 "go/ast"
10 "go/parser"
11 "go/token"
12 "go/types"
13 "log"
14
15 "golang.org/x/exp/typeparams"
16 )
17
18 const src = `
19 //!+input
20 package complex
21
22 type A interface{ ~string|~[]byte }
23
24 type B interface{ int|string }
25
26 type C interface { ~string|~int }
27
28 type D interface{ A|B; C }
29 //!-input
30 `
31
32 // !+print
33 func PrintNormalTerms(pkg *types.Package) error {
34 D := pkg.Scope().Lookup("D").Type()
35 terms, err := typeparams.NormalTerms(D)
36 if err != nil {
37 return err
38 }
39 for i, term := range terms {
40 if i > 0 {
41 fmt.Print("|")
42 }
43 fmt.Print(term)
44 }
45 fmt.Println()
46 return nil
47 }
48
49 //!-print
50
51 /*
52 //!+output
53 ~string|int
54 //!-output
55 */
56
57 func main() {
58 fset := token.NewFileSet()
59 f, err := parser.ParseFile(fset, "p.go", src, 0)
60 if err != nil {
61 log.Fatal(err)
62 }
63 conf := types.Config{}
64 pkg, err := conf.Check("typesets", fset, []*ast.File{f}, nil)
65 if err != nil {
66 log.Fatal(err)
67 }
68 if err := PrintNormalTerms(pkg); err != nil {
69 log.Fatal(err)
70 }
71 }