types

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package types is the checker's type language (section 4): primitives, null, void, never, arrays, maps, sets, functions, anonymous records, named types, type parameters, and unions.

Index

Constants

This section is empty.

Variables

View Source
var BuiltinKinds = map[string]value.Kind{
	"int8": value.KindInt8, "int16": value.KindInt16, "int32": value.KindInt32, "int64": value.KindInt64,
	"uint8": value.KindUint8, "uint16": value.KindUint16, "uint32": value.KindUint32, "uint64": value.KindUint64,
	"float32": value.KindFloat32, "float64": value.KindFloat64, "int": value.KindInt64, "byte": value.KindUint8,
	"bool": value.KindBool, "string": value.KindString,
}

BuiltinKinds maps the primitive type names, int and byte included, to their kinds (section 4, Primitives).

Functions

func Assignable

func Assignable(s, t Type) bool

Assignable reports whether a value of type s can be used where t is expected (section 4, Assignability). Equal types are tried first, once: it is the common case, and the walk below then compares with Equal only where it reaches a type that must be the same, so that a deep function type is related in linear time.

func BareBoundError

func BareBoundError(p *ParamDecl, q Param) string

BareBoundError is the message for a type parameter p whose bound is the type parameter q.

func Bindings

func Bindings(params []*ParamDecl, args []Type) map[*ParamDecl]Type

Bindings pairs type parameters with their arguments; a parameter past the arguments is left out.

func BuiltinArity

func BuiltinArity(name string) (int, bool)

BuiltinArity reports whether name is a built-in type name, a primitive, null, void, never, Array, Map, Set, Bytes, or Html, and how many type arguments it takes.

func BuiltinName

func BuiltinName(name string) bool

BuiltinName reports whether name is a built-in type name.

func CheckArgs

func CheckArgs(n Named) error

CheckArgs forms the unions that n's arguments make in its declaration, and in every instantiation that declaration reaches, however deep (section 4, Unions): Outer[M] fails when the Box[M] in its field does.

func CheckBounds

func CheckBounds(n Named) error

CheckBounds checks each argument of n against its parameter's bound, read with n's arguments in place of the parameters it names: the bound of a generic type's parameter holds wherever the type is written (section 4, Generics). A bound that substituting the arguments makes invalid is left to the error that substitution reports elsewhere.

func CheckUnion

func CheckUnion(members []Type) error

CheckUnion reports an error when the members of a formed union, which may name an alias whose body was not yet known when the union was formed, share a discriminator.

func CheckWritten

func CheckWritten(t Type) error

CheckWritten applies CheckArgs to every instantiation written in t, its arguments before it, as the checker resolves them: Opt[Either[Box[int], Box[string]]] fails on the Either, which forming the union in the body of Opt would expand.

func CheckWrittenBounds

func CheckWrittenBounds(t Type) error

CheckWrittenBounds applies CheckBounds to every instantiation written in t, its arguments before it.

func Components

func Components(root *Decl, succ func(d *Decl, edge func(*Decl)), skip func(*Decl) bool, done func(comp []*Decl, cyclic bool))

Components finds the strongly connected components of the declarations reached from root along the edges succ lists, by Tarjan's method in time linear in the edges, and calls done with each, a component before any that reaches it; the last is root's. A declaration skip reports, when first reached, is left out with its edges: one decided by an earlier search, or one that cannot be on a cycle through root. Cyclic reports that the component has more than one declaration, or one with an edge to itself.

A declaration refers to itself only through the declarations of its own component, so a search for such a reference can stop at every other one: following each declaration a chain of aliases reaches, from each of its links, would take time quadratic in its length.

func DeclRefs

func DeclRefs(d *Decl, f func(*Decl))

DeclRefs calls f with each declaration that d's alias body or field types name, the members of a union as written included.

func DeclValue

func DeclValue(d *Decl) value.Declaration

DeclValue is a declaration as a value, Reflect.Decl, compared by the declaration's identity.

func Discriminator

func Discriminator(t Type) (key, describe string, ok bool)

Discriminator is what is tests at run time: a kind, a tag, or a shape class, as a key that two types share exactly when is cannot tell them apart, and as words for a message. A sync type has none, since is does not accept one (section 4, Unions).

func Equal

func Equal(a, b Type) bool

Equal reports whether two types are the same type. A recursive alias is compared as the infinite type it stands for: a pair of types already under comparison is assumed equal, as == does for values, so that type L[T] = Array[L[T] | int] ends.

func FitsBound

func FitsBound(t, bound Type) bool

FitsBound reports whether t may stand for a type parameter whose bound, read with the other parameters' solutions in place, is bound (section 4, Generics): a single member of a union bound, or a type assignable to any other bound. A bounded type parameter fits any bound its own bound is assignable to, since it stands for one member of its own.

func HasParam

func HasParam(t Type) bool

HasParam reports whether t mentions any type parameter at all.

func Hash

func Hash(t Type) uint64

Hash agrees with Equal: equal types hash alike. It expands aliases and hashes a bounded depth of the structure, so a recursive alias such as Json.Value terminates.

func Includes

func Includes(t, m Type) bool

Includes reports whether t is a union that has a member equal to m, or is m itself.

func IsInterface

func IsInterface(t Type) bool

IsInterface reports whether t is an interface, aliases expanded.

func IsInvalid

func IsInvalid(t Type) bool

IsInvalid reports whether t is the error-recovery type, or a type parameter whose bound the checker rejected and made invalid, as that of T in func f[T: T] is: the error was reported once, at the bound, and what the body does with a T would only repeat it. An alias whose chain of alias bodies ends in Invalid counts too: the checker drops the body of an alias it rejected as cyclic, and each use of it, or of a parameter bounded by it, would only repeat that error.

func IsNever

func IsNever(t Type) bool

IsNever reports whether t is never.

func MemberOf

func MemberOf[T Type](t Type) (T, bool)

MemberOf returns the first member of t, or t itself when it is no union, that expands to a T.

func Mentions

func Mentions(t Type, set map[*ParamDecl]bool) bool

Mentions reports whether t mentions any type parameter in the set.

func MentionsOther

func MentionsOther(t Type, set map[*ParamDecl]bool) bool

MentionsOther reports whether t mentions a type parameter outside the set.

func SelfArgs

func SelfArgs(d *Decl) string

SelfArgs describes how the generic record type d refers to itself with other type arguments than its own parameters in order, or returns "" when it does not, or when d is no record. It follows d's fields through the generic types they name, tracking which of their parameters stand for which of d's. Only a record's fields can lead a generic type back to itself, since a generic alias on a cycle through no record's field is rejected (GenericRecursive), so an alias is held to no rule of its own: a record on its cycle is.

func SelfArgsIn

func SelfArgsIn(d *Decl, in func(*Decl) bool) string

SelfArgsIn is SelfArgs following only the declarations in reports, which must hold those of d's strongly connected component (Components); nil follows every declaration. The others cannot lead back to d.

func String

func String(t Type) string

String renders a type as Ant writes it.

func Strings

func Strings(where func(*Decl) string, ts ...Type) []string

Strings renders several types for one message. When a name stands for two different declarations among them, as two types of one name declared in different blocks or modules do, each use of it is followed by where(decl).

func UnionSelf

func UnionSelf(d *Decl) string

UnionSelf describes how the alias d names itself in a union inside its body, which is an error when the body is a union or names a type, or returns "". The outer union would need the inner one flattened, and the inner one the alias expanded, which forms the outer union again; a body that is an array, map, set, function or record type is not expanded to form a union, so type L = Array[L | int] is fine (section 4, Type declarations).

func UnionSelfIn

func UnionSelfIn(d *Decl, in func(*Decl) bool) string

UnionSelfIn is UnionSelf following only the bodies of the declarations in reports, which must hold those of d's strongly connected component (Components); nil follows every body.

func Walk

func Walk(t Type, f func(Type))

Walk calls f on t and on every type written inside it, the type arguments of a named type included, but not on the bodies or fields of the declarations it names.

Types

type ArgsMemo

type ArgsMemo struct {

	// Only, when set, limits the memo to the declarations it accepts, those whose bodies, and
	// the bodies of all they reach, no longer change and mention no type parameter but their own.
	Only func(*Decl) bool
	// contains filtered or unexported fields
}

ArgsMemo remembers, from one call of its CheckArgs or CheckWritten to the next, the generic declarations whose instantiation with distinct type parameters as arguments was found valid, as the body of D[T] = Array[C[T]] instantiates C. Each such instantiation of a declaration is valid exactly when every other one is: forming a union tells type parameters apart by identity alone, and the bodies an instantiation reaches mention no type parameter but their own, which substitution replaces. So a chain of such declarations is validated once, not once from each link. A memo may be used only while the declarations it reaches do not change, and their type parameters are their own; a nil memo remembers nothing.

func (*ArgsMemo) CheckArgs

func (m *ArgsMemo) CheckArgs(n Named) error

CheckArgs is the function CheckArgs, remembering what it validates when n is valid. One that fails may have assumed, along the way, that the instantiation it started from holds.

func (*ArgsMemo) CheckWritten

func (m *ArgsMemo) CheckWritten(t Type) error

CheckWritten is the function CheckWritten with m's CheckArgs.

type Array

type Array struct {
	Elem Type
}

Array is Array[T].

type Decl

type Decl struct {
	Name   string
	Kind   DeclKind
	Params []*ParamDecl
	Fields []Field    // RecordDecl: the fields, with Params inside
	Base   value.Kind // PrimitiveDecl: the base kind; KindOpaqueDecl: the kind
	Body   Type       // AliasDecl: the body, with Params inside
	Funcs  map[string]*Func
	Pos    any    // the declaration's position, for messages
	Path   string // the file the declaration is in, for messages
	// Self is the type parameter that stands for the satisfying type in an interface's
	// signatures, and Order lists its functions in declaration order. BoundOnly marks an
	// interface that mentions Self beyond the receiver, which is a bound and not a type.
	Self      *ParamDecl
	Order     []string
	BoundOnly bool
	// Satisfies, set by the checker on an interface, reports whether s satisfies the interface
	// instantiated with args. rel relates two types as the assignability under way does, so that
	// a pair under test holds (Assignable).
	Satisfies func(s Type, args []Type, rel func(a, b Type) bool) bool
	// Tag is the tag the runtime binds the type to, set by the interpreter on the declarations
	// it builds for reflection (section 4, Types as values); nil in the checker and for a
	// KindOpaqueDecl, whose values carry no tag.
	Tag *value.Tag
}

Decl is a type declaration.

func AliasCycle

func AliasCycle(d *Decl) (path []*Decl, cyclic bool)

AliasCycle reports whether the alias d reaches itself through alias bodies and unions alone, without passing through a record, array, map, set, or function (section 4, Type declarations), and the other aliases on the way.

func AliasCycleError

func AliasCycleError(d *Decl) (blame *Decl, msg string, cyclic bool)

AliasCycleError describes the cycle the alias d is in when it reaches itself without passing through a record, array, map, set, or function (AliasCycle), or returns cyclic false. The message names the member of the cycle declared first, whichever member was resolved first, so the checker and the runtime blame the same declaration, and lists the other members in the order the cycle passes through them. blame is that member.

func AliasCycleErrorIn

func AliasCycleErrorIn(d *Decl, in func(*Decl) bool) (blame *Decl, msg string, cyclic bool)

AliasCycleErrorIn is AliasCycleError following only the bodies of the declarations in reports, which must hold those of d's strongly connected component (Components); nil follows every body.

func AliasCycleIn

func AliasCycleIn(d *Decl, in func(*Decl) bool) (path []*Decl, cyclic bool)

AliasCycleIn is AliasCycle following only the bodies of the declarations in reports, which must hold those of d's strongly connected component (Components); nil follows every body.

func GenericRecursive

func GenericRecursive(d *Decl) (blame *Decl, msg string, cyclic bool)

GenericRecursive describes the generic alias on a cycle of aliases through d, or returns cyclic false. An alias is recursive when it lies on a cycle of the graph whose edges lead from an alias to every declaration its body names, type arguments included, and from a record to nothing: a record's fields are never expanded, so recursion through them is finite, but a record's type arguments are compared, so they do not break a cycle. A bound is no edge either, since nothing expands it. A recursive alias takes no type parameters, and neither does any alias on its cycle, since a generic alias that meets its own instantiation would have to be substituted without end (section 4, Type declarations). blame is the generic alias of the cycle declared first, so that the checker and the runtime blame the same declaration, and the message lists the other aliases the cycle passes through from it.

func GenericRecursiveIn

func GenericRecursiveIn(d *Decl, in func(*Decl) bool) (blame *Decl, msg string, cyclic bool)

GenericRecursiveIn is GenericRecursive following only the declarations in reports, which must hold those of d's strongly connected component (Components); nil follows every declaration.

type DeclKind

type DeclKind int

DeclKind says what a type declaration declared.

const (
	RecordDecl       DeclKind = iota // a named record type
	PrimitiveDecl                    // a named primitive type
	OpaqueDecl                       // a prelude type with no structure, such as Mutex
	KindOpaqueDecl                   // an opaque prelude type whose values carry a kind of their own, Bytes and Html: Base holds the kind, and is tests it by name
	TaggedOpaqueDecl                 // an opaque type whose values carry its tag, as the time types of stdlib.md and Regex.Pattern do: is tests it by the tag
	AliasDecl                        // an alias, interchangeable with its body
	InterfaceDecl                    // an interface (section 4, Interfaces): Funcs holds its signatures, each taking Self first
)

type DuplicateError added in v0.2.0

type DuplicateError struct {
	A, B Type
	Kind string // what both are, as "an array"
}

A DuplicateError is a union whose members A and B share a discriminator, as Array[int] and Array[string] do: is could not tell them apart.

func (*DuplicateError) Error added in v0.2.0

func (e *DuplicateError) Error() string

func (*DuplicateError) Merged added in v0.2.0

func (e *DuplicateError) Merged() (Type, bool)

Merged is the one type that holds the values of both members, when they are the same generic type with different arguments: Array[int | string] for Array[int] and Array[string].

type Field

type Field struct {
	Name string
	Type Type
	// Default marks a field of a RecordDecl that declares a default, which a literal may leave
	// out (section 4, Field defaults).
	Default bool
}

Field is a field of a record type.

func FieldsOf

func FieldsOf(t Type) []Field

FieldsOf returns the fields a type exposes, or nil when it is not a record type.

type Func

type Func struct {
	Params   []Type
	Return   Type
	Required int
	Names    []string // parameter names, for named arguments; nil for a function type
}

Func is a function type. Required is the number of parameters without defaults; a declared function may be called with fewer arguments than Params down to Required.

type Invalid

type Invalid struct{}

Invalid stands for a type that could not be determined; it is assignable both ways so that one error does not cascade.

type Map

type Map struct {
	Key, Val Type
}

Map is Map[K, V].

type Named

type Named struct {
	Decl *Decl
	Args []Type
}

Named is a reference to a declared type with type arguments.

func (Named) FieldsOf

func (n Named) FieldsOf() []Field

FieldsOf returns the fields of a named record type with its arguments substituted. The substitution cannot fail: the checker checks every instantiation a written type reaches, through fields and alias bodies, where its arguments are written (section 4, Unions), and a type value is built only from closed type expressions and builders that keep this (section 4, Types as values).

type Never

type Never struct{}

Null, Void, and Never are the types of null, void, and expressions that do not return.

type Null

type Null struct{}

Null, Void, and Never are the types of null, void, and expressions that do not return.

type Param

type Param struct {
	Decl *ParamDecl
}

Param is a type parameter in use.

func BareBound

func BareBound(bound Type) (Param, bool)

BareBound reports the type parameter a bound is, aliases expanded, when it is one: a bound may not itself be a type parameter, its own or another's (section 4, Generics), since reading it would lead from the parameter back to a parameter with nothing else to go on, as T: T does.

type ParamDecl

type ParamDecl struct {
	Name  string
	Bound Type // nil when unbounded
}

ParamDecl is a type parameter declaration.

type Prim

type Prim struct {
	Kind value.Kind
}

Prim is one of the ten numeric kinds, bool, or string.

type Record

type Record struct {
	Fields []Field
}

Record is an anonymous record type { name: T, ... }.

type Set

type Set struct {
	Elem Type
}

Set is Set[T].

type Type

type Type interface {
	// contains filtered or unexported methods
}

Type is a static type. Named types are compared by declaration identity and arguments; everything else is structural.

var (
	IntType     Type = Prim{value.KindInt64}
	Float64Type Type = Prim{value.KindFloat64}
	BoolType    Type = Prim{value.KindBool}
	StringType  Type = Prim{value.KindString}
	ByteType    Type = Prim{value.KindUint8}
)

Common types.

func Builtin

func Builtin(name string, args []Type, opaque func(name string) *Decl) Type

Builtin makes the type a built-in name denotes from its resolved type arguments, as many as BuiltinArity gives. opaque gives the declaration of Bytes or Html, which each resolver keeps.

func DeclBody

func DeclBody(d *Decl) []Type

DeclBody returns the types a generic declaration substitutes its arguments into: an alias's body or a record's field types. It is empty for a declaration without type parameters.

func Expand

func Expand(t Type) Type

Expand replaces an alias by its body, repeatedly, and leaves every other type alone. The substitution cannot fail: the checker checks an alias's arguments against its body where they are written, and a generic alias reaches itself only through a record's field (section 4, Type declarations), which expansion never enters, so no deeper instantiation is left unchecked. Nor can the chain loop, since the checker marks a cyclic alias invalid as soon as its cycle is resolved; past 64 steps the instantiations met are tracked to assert it.

func ExpandOK

func ExpandOK(t Type) (Type, bool)

ExpandOK is Expand for a type the checks of its declarations have not all reached yet, such as a bound read while the declarations are resolved: it reports false where Expand would panic, on an alias cycle or arguments its body rejects, and expands an alias whose body is not yet resolved to itself.

func Join

func Join(ts ...Type) (Type, error)

Join forms the union of branch types where control merges, or returns an error to report. A member that satisfies an interface among the others is absorbed by it: the join of a Circle and a Shape is the Shape, as the value narrowed in one branch is a Shape still in the other (section 7, Narrowing).

func MakeUnion

func MakeUnion(members []Type) (Type, error)

MakeUnion forms the union of members: flattened, deduplicated, never dropped. It reports an error when two members share a discriminator (section 4, Unions).

func Members

func Members(t Type) []Type

Members returns a union's members, or the type itself as one member.

func Nullable

func Nullable(t Type) Type

Nullable is t | null.

func Subst

func Subst(t Type, m map[*ParamDecl]Type) (Type, error)

Subst replaces type parameters by their bindings. Unions are formed again after substitution, which can fail where the arguments are written (section 4, Unions).

func Without

func Without(t Type, m Type) Type

Without returns t with the members equal to m removed; never when nothing remains.

type TypeValue

type TypeValue struct {
	T Type
}

TypeValue is a type as a run-time value (section 4, Types as values): what a type expression evaluates to and what the Reflect functions pass around. It implements value.TypeValue, so the runtime compares, hashes, and renders type values with the checker's own relations.

func (*TypeValue) EqualType

func (v *TypeValue) EqualType(o value.TypeValue) bool

EqualType reports whether two type values denote the same type.

func (*TypeValue) HashType

func (v *TypeValue) HashType() uint64

HashType agrees with EqualType.

func (*TypeValue) String

func (v *TypeValue) String() string

String renders the type in annotation syntax.

type Union

type Union struct {
	Members []Type
	Written []Type
}

Union is A | B. It is always normalized: flat, without duplicates or never, and with at least two members. Written keeps the members as they were written when one of them was an alias that normalization expanded, so that messages can name the alias; it is nil otherwise and takes no part in equality.

type Void

type Void struct{}

Null, Void, and Never are the types of null, void, and expressions that do not return.

Jump to

Keyboard shortcuts

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