typeparams

package
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

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

View Source
const Enabled = false

Enabled reports whether type parameters are enabled in the current build environment.

Variables

This section is empty.

Functions

func ForFuncDecl

func ForFuncDecl(*ast.FuncDecl) *ast.FieldList

ForFuncDecl extracts the (possibly nil) type parameter node list from n.

func ForNamed

func ForNamed(*types.Named) []*types.TypeName

ForNamed extracts the (possibly empty) type parameter object list from named.

func ForSignature

func ForSignature(*types.Signature) []*types.TypeName

ForSignature extracts the (possibly empty) type parameter object list from sig.

func ForTypeDecl

func ForTypeDecl(*ast.TypeSpec) *ast.FieldList

ForTypeDecl extracts the (possibly nil) type parameter node list from n.

func GetInferred

func GetInferred(*types.Info, ast.Expr) ([]types.Type, *types.Signature)

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

func InitInferred(*types.Info)

InitInferred initializes info to record inferred type information.

func IsComparable

func IsComparable(*types.Interface) bool

IsComparable reports if iface is the comparable interface.

func IsConstraint

func IsConstraint(*types.Interface) bool

IsConstraint reports whether iface may only be used as a type parameter constraint (i.e. has a type set or is the comparable interface).

func NamedTArgs

func NamedTArgs(*types.Named) []types.Type

NamedTArgs extracts the (possibly empty) type argument list from named.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL