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 ¶
- Variables
- func Assignable(s, t Type) bool
- func BareBoundError(p *ParamDecl, q Param) string
- func Bindings(params []*ParamDecl, args []Type) map[*ParamDecl]Type
- func BuiltinArity(name string) (int, bool)
- func BuiltinName(name string) bool
- func CheckArgs(n Named) error
- func CheckBounds(n Named) error
- func CheckUnion(members []Type) error
- func CheckWritten(t Type) error
- func CheckWrittenBounds(t Type) error
- func Components(root *Decl, succ func(d *Decl, edge func(*Decl)), skip func(*Decl) bool, ...)
- func DeclRefs(d *Decl, f func(*Decl))
- func DeclValue(d *Decl) value.Declaration
- func Discriminator(t Type) (key, describe string, ok bool)
- func Equal(a, b Type) bool
- func FitsBound(t, bound Type) bool
- func HasParam(t Type) bool
- func Hash(t Type) uint64
- func Includes(t, m Type) bool
- func IsInterface(t Type) bool
- func IsInvalid(t Type) bool
- func IsNever(t Type) bool
- func MemberOf[T Type](t Type) (T, bool)
- func Mentions(t Type, set map[*ParamDecl]bool) bool
- func MentionsOther(t Type, set map[*ParamDecl]bool) bool
- func SelfArgs(d *Decl) string
- func SelfArgsIn(d *Decl, in func(*Decl) bool) string
- func String(t Type) string
- func Strings(where func(*Decl) string, ts ...Type) []string
- func UnionSelf(d *Decl) string
- func UnionSelfIn(d *Decl, in func(*Decl) bool) string
- func Walk(t Type, f func(Type))
- type ArgsMemo
- type Array
- type Decl
- func AliasCycle(d *Decl) (path []*Decl, cyclic bool)
- func AliasCycleError(d *Decl) (blame *Decl, msg string, cyclic bool)
- func AliasCycleErrorIn(d *Decl, in func(*Decl) bool) (blame *Decl, msg string, cyclic bool)
- func AliasCycleIn(d *Decl, in func(*Decl) bool) (path []*Decl, cyclic bool)
- func GenericRecursive(d *Decl) (blame *Decl, msg string, cyclic bool)
- func GenericRecursiveIn(d *Decl, in func(*Decl) bool) (blame *Decl, msg string, cyclic bool)
- type DeclKind
- type DuplicateError
- type Field
- type Func
- type Invalid
- type Map
- type Named
- type Never
- type Null
- type Param
- type ParamDecl
- type Prim
- type Record
- type Set
- type Type
- func Builtin(name string, args []Type, opaque func(name string) *Decl) Type
- func DeclBody(d *Decl) []Type
- func Expand(t Type) Type
- func ExpandOK(t Type) (Type, bool)
- func Join(ts ...Type) (Type, error)
- func MakeUnion(members []Type) (Type, error)
- func Members(t Type) []Type
- func Nullable(t Type) Type
- func Subst(t Type, m map[*ParamDecl]Type) (Type, error)
- func Without(t Type, m Type) Type
- type TypeValue
- type Union
- type Void
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
BareBoundError is the message for a type parameter p whose bound is the type parameter q.
func Bindings ¶
Bindings pairs type parameters with their arguments; a parameter past the arguments is left out.
func BuiltinArity ¶
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 ¶
BuiltinName reports whether name is a built-in type name.
func CheckArgs ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Hash ¶
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 IsInterface ¶
IsInterface reports whether t is an interface, aliases expanded.
func IsInvalid ¶
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 MemberOf ¶
MemberOf returns the first member of t, or t itself when it is no union, that expands to a T.
func MentionsOther ¶
MentionsOther reports whether t mentions a type parameter outside the set.
func SelfArgs ¶
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 ¶
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 Strings ¶
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 ¶
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 ¶
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.
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 ¶
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 ¶
CheckWritten is the function CheckWritten with m's CheckArgs.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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.
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 Named ¶
Named is a reference to a declared type with type arguments.
func (Named) FieldsOf ¶
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.
type Record ¶
type Record struct {
Fields []Field
}
Record is an anonymous record type { name: 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
MakeUnion forms the union of members: flattened, deduplicated, never dropped. It reports an error when two members share a discriminator (section 4, Unions).
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.
type Union ¶
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.