Help us shape the future of Go! Take the 2026 Go Developer Survey

typeparams

package module
v0.0.0-...-f0251eb Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: BSD-3-Clause Imports: 7 Imported by: 50

Documentation

Overview

Package typeparams contains common utilities for writing tools that interact with generic Go code, as introduced with Go 1.18.

Many of the types and functions in this package are proxies for the new APIs introduced in the standard library with Go 1.18. For example, the typeparams.Union type is an alias for go/types.Union, and the ForTypeSpec function returns the value of the go/ast.TypeSpec.TypeParams field.

Additionally, this package contains common utilities for working with the new generic constructs, to supplement the standard library APIs. Notably, the NormalTerms API computes a minimal representation of the structural restrictions on a type parameter. In the future, these supplemental APIs may be available in the standard library..

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyTypeSet = errors.New("empty type set")

ErrEmptyTypeSet is returned if a type set computation results in a type set with no types.

Functions

func Enabled deprecated

func Enabled() bool

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

Deprecated: Type parameters are always enabled. Or use go fix (after go1.26).

func ForFuncType

func ForFuncType(n *ast.FuncType) *ast.FieldList

ForFuncType returns n.TypeParams.

func ForTypeSpec

func ForTypeSpec(n *ast.TypeSpec) *ast.FieldList

ForTypeSpec returns n.TypeParams.

func GenericAssignableTo

func GenericAssignableTo(ctxt *Context, V, T types.Type) bool

GenericAssignableTo is a generalization of types.AssignableTo that implements the following rule for uninstantiated generic types:

If V and T are generic named types, then V is considered assignable to T if, for every possible instantiation of V[A_1, ..., A_N], the instantiation T[A_1, ..., A_N] is valid and V[A_1, ..., A_N] implements T[A_1, ..., A_N].

If T has structural constraints, they must be satisfied by V.

For example, consider the following type declarations:

type Interface[T any] interface {
	Accept(T)
}

type Container[T any] struct {
	Element T
}

func (c Container[T]) Accept(t T) { c.Element = t }

In this case, GenericAssignableTo reports that instantiations of Container are assignable to the corresponding instantiation of Interface.

func GetInstances deprecated

func GetInstances(info *types.Info) map[*ast.Ident]Instance

GetInstances returns info.Instances.

Deprecated: Use info.Instances instead.

func InitInstances deprecated

func InitInstances(info *types.Info)

InitInstances initializes info to record information about type and function instances.

Deprecated: Use info.Instances = make(...) instead.

func Instantiate deprecated

func Instantiate(ctxt *Context, typ types.Type, targs []types.Type, validate bool) (types.Type, error)

Instantiate calls types.Instantiate.

Deprecated: Use go/types.Instantiate instead.

func IsComparable deprecated

func IsComparable(iface *types.Interface) bool

IsComparable calls iface.IsComparable().

Deprecated: Use iface.IsComparable() instead.

func IsImplicit deprecated

func IsImplicit(iface *types.Interface) bool

IsImplicit calls iface.IsImplicit().

Deprecated: Use iface.IsImplicit() instead.

func IsMethodSet deprecated

func IsMethodSet(iface *types.Interface) bool

IsMethodSet calls iface.IsMethodSet().

Deprecated: Use iface.IsMethodSet() instead.

func IsTypeParam

func IsTypeParam(t types.Type) bool

IsTypeParam reports whether t is a type parameter.

func MarkImplicit deprecated

func MarkImplicit(iface *types.Interface)

MarkImplicit calls iface.MarkImplicit().

Deprecated: Use iface.MarkImplicit() instead.

func NamedTypeOrigin deprecated

func NamedTypeOrigin(named *types.Named) types.Type

NamedTypeOrigin returns named.Orig().

Deprecated: Use named.Origin() instead.

func NewSignatureType deprecated

func NewSignatureType(recv *types.Var, recvTypeParams, typeParams []*TypeParam, params, results *types.Tuple, variadic bool) *types.Signature

NewSignatureType calls types.NewSignatureType.

Deprecated: Use go/types.NewSignatureType instead.

func OriginMethod

func OriginMethod(fn *types.Func) *types.Func

OriginMethod returns the origin method associated with the method fn. For methods on a non-generic receiver base type, this is just fn. However, for methods with a generic receiver, OriginMethod returns the corresponding method in the method set of the origin type.

As a special case, if fn is not a method (has no receiver), OriginMethod returns fn.

func PackIndexExpr

func PackIndexExpr(x ast.Expr, lbrack token.Pos, indices []ast.Expr, rbrack token.Pos) ast.Expr

PackIndexExpr returns an *ast.IndexExpr or *ast.IndexListExpr, depending on the cardinality of indices. Calling PackIndexExpr with len(indices) == 0 will panic.

func SetForNamed deprecated

func SetForNamed(n *types.Named, tparams []*TypeParam)

SetForNamed sets the type params tparams on n. Each tparam must be of dynamic type *types.TypeParam.

Deprecated: Use n.SetTypeParams(...) instead.

func UnpackIndexExpr

func UnpackIndexExpr(n ast.Node) (x ast.Expr, lbrack token.Pos, indices []ast.Expr, rbrack token.Pos)

UnpackIndexExpr extracts data from AST nodes that represent index expressions.

For an ast.IndexExpr, the resulting indices slice will contain exactly one index expression. For an ast.IndexListExpr (go1.18+), it may have a variable number of index expressions.

For nodes that don't represent index expressions, the first return value of UnpackIndexExpr will be nil.

Types

type Context deprecated

type Context = types.Context

Context is an alias for types.Context.

Deprecated: Use go/types.Context instead.

func NewContext deprecated

func NewContext() *Context

NewContext calls types.NewContext.

Deprecated: Use go/types.NewContext instead.

type IndexListExpr deprecated

type IndexListExpr = ast.IndexListExpr

IndexListExpr is an alias for ast.IndexListExpr.

Deprecated: Use go/ast.IndexListExpr instead.

type Instance deprecated

type Instance = types.Instance

Instance is an alias for types.Instance.

Deprecated: Use go/types.Instance instead.

type Term deprecated

type Term = types.Term

Term is an alias for types.Term.

Deprecated: Use go/types.Term instead.

func NewTerm deprecated

func NewTerm(tilde bool, typ types.Type) *Term

NewTerm calls types.NewTerm.

Deprecated: Use go/types.NewTerm instead.

func NormalTerms

func NormalTerms(typ types.Type) ([]*Term, error)

NormalTerms returns a slice of terms representing the normalized structural type restrictions of a type, if any.

For all types whose underlying type is not *types.TypeParam, *types.Interface, or *types.Union, this is just a single term with Tilde() == false and Type() == typ. For types whose underlying type is *types.TypeParam, *types.Interface, and *types.Union, see below.

Structural type restrictions of a type parameter are created via non-interface types embedded in its constraint interface (directly, or via a chain of interface embeddings). For example, in the declaration type T[P interface{~int; m()}] int is the structural restriction of the type parameter P is ~int.

With interface embedding and unions, the specification of structural type restrictions may be arbitrarily complex. For example, consider the following:

type A interface{ ~string|~[]byte }

type B interface{ int|string }

type C interface { ~string|~int }

type T[P interface{ A|B; C }] int

In this example, the structural type restriction of P is ~string|int: A|B expands to ~string|~[]byte|int|string, which reduces to ~string|~[]byte|int, which when intersected with C (~string|~int) yields ~string|int.

NormalTerms computes these expansions and reductions, producing a "normalized" form of the embeddings. A structural restriction is normalized if it is a single union containing no interface terms, and is minimal in the sense that removing any term changes the set of types satisfying the constraint. It is left as a proof for the reader that, modulo sorting, there is exactly one such normalized form.

Because the minimal representation always takes this form, NormalTerms returns a slice of tilde terms corresponding to the terms of the union in the normalized structural restriction. An error is returned if the type is invalid, exceeds complexity bounds, or has an empty type set. In the latter case, NormalTerms returns ErrEmptyTypeSet.

NormalTerms makes no guarantees about the order of terms, except that it is deterministic.

type TypeList deprecated

type TypeList = types.TypeList

TypeList is an alias for types.TypeList

Deprecated: Use go/types.TypeList instead.

func NamedTypeArgs deprecated

func NamedTypeArgs(named *types.Named) *TypeList

NamedTypeArgs returns named.TypeArgs().

Deprecated: Use named.TypeArgs() instead.

type TypeParam deprecated

type TypeParam = types.TypeParam

TypeParam is an alias for types.TypeParam

Deprecated: Use go/types.TypeParam instead.

func NewTypeParam deprecated

func NewTypeParam(name *types.TypeName, constraint types.Type) *TypeParam

NewTypeParam calls types.NewTypeParam.

Deprecated: Use go/types.NewTypeParam instead.

type TypeParamList deprecated

type TypeParamList = types.TypeParamList

TypeParamList is an alias for types.TypeParamList

Deprecated: Use go/types.TypeParamList instead.

func ForNamed deprecated

func ForNamed(named *types.Named) *TypeParamList

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

Deprecated: Use named.TypeParams() instead.

func ForSignature deprecated

func ForSignature(sig *types.Signature) *TypeParamList

ForSignature returns sig.TypeParams()

Deprecated: Use sig.TypeParams() instead.

func RecvTypeParams deprecated

func RecvTypeParams(sig *types.Signature) *TypeParamList

RecvTypeParams returns sig.RecvTypeParams().

Deprecated: Use sig.RecvTypeParams() instead.

type Union deprecated

type Union = types.Union

Union is an alias for types.Union

Deprecated: Use go/types.Union instead.

func NewUnion deprecated

func NewUnion(terms []*Term) *Union

NewUnion calls types.NewUnion.

Deprecated: Use go/types.NewUnion instead.

Directories

Path Synopsis
example
findtypeparams command
genericmethods command
implicit command
instantiation command
interfaces command
methoddecls command
predicates command
typesets command

Jump to

Keyboard shortcuts

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