Documentation
¶
Overview ¶
Package typeparams provides functions to work indirectly with type parameter data stored in go/ast and go/types objects, while these API are guarded by a build constraint.
This package exists to make it easier for tools to work with generic code, while also compiling against older Go versions.
Index ¶
- Constants
- func ForFuncDecl(*ast.FuncDecl) *ast.FieldList
- func ForNamed(*types.Named) []*types.TypeName
- func ForSignature(*types.Signature) []*types.TypeName
- func ForTypeDecl(*ast.TypeSpec) *ast.FieldList
- func GetInferred(*types.Info, ast.Expr) ([]types.Type, *types.Signature)
- func InitInferred(*types.Info)
- func IsComparable(*types.Interface) bool
- func IsConstraint(*types.Interface) bool
- func NamedTArgs(*types.Named) []types.Type
- type IndexExprData
Constants ¶
const Enabled = false
Enabled reports whether type parameters are enabled in the current build environment.
Variables ¶
This section is empty.
Functions ¶
func ForFuncDecl ¶
ForFuncDecl extracts the (possibly nil) type parameter node list from n.
func ForSignature ¶
ForSignature extracts the (possibly empty) type parameter object list from sig.
func ForTypeDecl ¶
ForTypeDecl extracts the (possibly nil) type parameter node list from n.
func GetInferred ¶
GetInferred extracts inferred type information from info for e.
The expression e may have an inferred type if it is an *ast.IndexExpr representing partial instantiation of a generic function type for which type arguments have been inferred using constraint type inference, or if it is an *ast.CallExpr for which type type arguments have be inferred using both constraint type inference and function argument inference.
func InitInferred ¶
InitInferred initializes info to record inferred type information.
func IsComparable ¶
IsComparable reports if iface is the comparable interface.
func IsConstraint ¶
IsConstraint reports whether iface may only be used as a type parameter constraint (i.e. has a type set or is the comparable interface).
Types ¶
type IndexExprData ¶
type IndexExprData struct { X ast.Expr // expression Lbrack token.Pos // position of "[" Indices []ast.Expr // index expressions Rbrack token.Pos // position of "]" }
A IndexExprData holds data from both ast.IndexExpr and the new ast.MultiIndexExpr, which was introduced in Go 1.18.
func GetIndexExprData ¶
func GetIndexExprData(n ast.Node) *IndexExprData
GetIndexExprData extracts data from AST nodes that represent index expressions.
For an ast.IndexExpr, the resulting IndexExprData will have exactly one index expression. For an ast.MultiIndexExpr (go1.18+), it may have a variable number of index expressions.
For nodes that don't represent index expressions, GetIndexExprData returns nil.