nupp.types
Compiler-only type inspection and construction inside @comptime function declarations. Every member is an ordinary checked compile-time value; none has a runtime representation.
Module contents
Functions
| Function | Kind | Description |
|---|---|---|
affine | comptime function | An affine resource over representation: used at most once, and never copied. |
arguments | comptime function | The type arguments a generic declaration was applied to. |
array | comptime function | The Lua array {element}, with integer keys and no fixed length. |
borrowed | comptime function | A borrow of inner, valid for the region that borrows it and not stored. |
carray | comptime function | A C array of element, sized when count is given and unsized when it is not. |
constof | comptime function | inner behind a const capability, which permits reads and refuses writes. |
construction | comptime function | The parameters a record or struct is constructed with, as a pack whose slots carry their names. |
describe | comptime function | Every fact about one handle at once, as a record whose members are populated according to its kind. |
elements | comptime function | The fixed elements of a composite type, or a pack's fixed head. |
error | comptime function | Rejects this type application deliberately, with an authored message. |
fields | comptime function | The public fields of a shape or of a nominal record, interface, or struct. |
formatArguments | comptime function | Computes the arguments consumed by a literal Lua format string. |
function_ | comptime function | A function type from its parameter and result packs. |
indexer | comptime function | A type indexed by computed keys rather than by named fields. |
intersection | comptime function | The intersection of its members, as a set, flattened and deduplicated the way union is. |
kind | comptime function | What a handle is, as a string, which is the usual way to branch on one. |
length | comptime function | base narrowed to the byte lengths from min through max, both included. |
literal | comptime function | The singleton type inhabited by a value. |
map | comptime function | An indexer whose write side mirrors its read side unless stated otherwise. |
nonExhaustive | comptime function | The member that keeps a union open, so a switch over it stays inexhaustive. |
optional | comptime function | inner | nil. |
pack | comptime function | A type pack: a fixed head of slots, then an optional tail standing for however many values remain. |
parameters | comptime function | The parameter pack of a function type. |
pinned | comptime function | inner pinned to its address, so the collector may not move it. |
pointer | comptime function | A pointer to inner. |
range | comptime function | base narrowed to the values from min through max, both included. |
readAt | comptime function | The type read at a key: the named field when the key is a string literal naming a readable one, and otherwise the... |
readKeys | comptime function | Every key a type can be read at: one string literal per readable field, plus the indexer's read key when it has one. |
results | comptime function | The result pack of a function type. |
sameNominal | comptime function | Whether two types name the same nominal declaration, ignoring its arguments. |
shape | comptime function | A structural type with named fields, and optionally an indexer for the keys the names do not cover. |
tuple | comptime function | A fixed-length positional type, one slot per element in the order given. |
union | comptime function | The union of its members, as a set. |
writeAt | comptime function | The type written at a key: the named field when the key is a string literal naming a writable one, and otherwise the... |
writeKeys | comptime function | Every key a type can be written at: one string literal per writable field, plus the indexer's write key when it has one. |
Values
| Value | Kind | Description |
|---|---|---|
any | comptime type | The dynamic type; compatible with everything, checked at neither end. |
boolean | comptime type | true or false. |
cdata | comptime type | Any cdata value. |
cstring | comptime type | const char . |
float | comptime type | An established binary32 Lua number; widens to number. |
int16 | comptime type | Signed physical storage; loads as int32. |
int32 | comptime type | An established signed 32-bit Lua integer. |
int64 | comptime type | A boxed 64-bit signed cdata integer, as LuaJIT's 1LL. |
int8 | comptime type | Signed physical storage; loads as int32. |
integer | comptime type | A number known to be integral. |
never | comptime type | The bottom type; fits everything, nothing fits it. |
nil_ | comptime type | The nil singleton, spelled nil because nil is a keyword. |
number | comptime type | A LuaJIT double. |
string | comptime type | A Lua string. |
table_ | comptime type | Any table shape, compatible with every table type in both directions, spelled table because table names the standard... |
thread | comptime type | A coroutine. |
uint16 | comptime type | Unsigned physical storage; loads as uint32. |
uint32 | comptime type | An established unsigned 32-bit Lua integer. |
uint64 | comptime type | A boxed 64-bit unsigned cdata integer, as LuaJIT's 1ULL. |
uint8 | comptime type | Unsigned physical storage; loads as uint32. |
unknown | comptime type | The top type; everything fits it, it fits nothing else. |
userdata | comptime type | Userdata. |
voidptr | comptime type | void . |
Functions#
affinecomptime function#
local affine: function(representation: type): type
& function(representation: type, const terminal: function): typeAn affine resource over representation: used at most once, and never copied.
With a terminal, that const function is the operation consuming the resource, the way close consumes a file. Without one the resource is transfer-only, so ownership may move but nothing ends it.
Arguments
| Name | Type | Description |
|---|---|---|
representation | type | the underlying representation type |
terminal | any | the const-function parameter that consumes it |
Returns
| Type | Description |
|---|---|
type | the affine type |
Raises
| Type | Condition |
|---|---|
string | when the terminal is not a const-function parameter |
argumentscomptime function#
The type arguments a generic declaration was applied to.
Empty for a type that is not a generic application, so a caller may ask without knowing which it has.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | the applied type to inspect |
Returns
| Type | Description |
|---|---|
{type} | one handle per argument, in declaration order |
arraycomptime function#
local array: function(element: type): typeThe Lua array {element}, with integer keys and no fixed length.
Arguments
| Name | Type | Description |
|---|---|---|
element | type | the element type |
Returns
| Type | Description |
|---|---|
type | the array type |
borrowedcomptime function#
local borrowed: function(inner: type): typeA borrow of inner, valid for the region that borrows it and not stored.
Arguments
| Name | Type | Description |
|---|---|---|
inner | type | the owned type being borrowed |
Returns
| Type | Description |
|---|---|
type | the borrowed type |
carraycomptime function#
A C array of element, sized when count is given and unsized when it is not.
Arguments
| Name | Type | Description |
|---|---|---|
element | type | the element type |
count | integer? | the fixed length, or nil for an unsized array |
Returns
| Type | Description |
|---|---|
type | the C array type |
Raises
| Type | Condition |
|---|---|
string | when the count is not a non-negative exact integer |
constofcomptime function#
local constof: function(inner: type): typeinner behind a const capability, which permits reads and refuses writes.
Arguments
| Name | Type | Description |
|---|---|---|
inner | type | the type to make const |
Returns
| Type | Description |
|---|---|
type | the const type |
constructioncomptime function#
local construction: function(value: type): typepackThe parameters a record or struct is constructed with, as a pack whose slots carry their names.
The one declared constructor's parameters, or the stored fields in declaration order when the declaration has no constructor; a field with a default, or one that admits nil, is an optional slot. Written as ...: unpackof nupp.types.construction(E), the pack is a parameter list a call binds by position or by name, the way it would for new E(...).
Arguments
| Name | Type | Description |
|---|---|---|
value | type | the record or struct to inspect |
Returns
| Type | Description |
|---|---|
typepack | its construction contract |
Raises
| Type | Condition |
|---|---|
string | when the declaration has several constructors, which is no single contract |
describecomptime function#
local describe: function(value: type | typepack): {
kind: string,
sourceKind: string?,
--- Reports the nominal declaration category without expanding a reference.
declarationKind: string?,
name: string?,
value: (string | boolean | integer)?,
element: type?,
members: {type}?,
fields: {{
name: string,
read: type?,
write: type?
}}?,
indexer: type?,
readKey: type?,
readValue: type?,
writeKey: type?,
writeValue: type?,
nominal: boolean?,
head: {type}?,
tail: type?,
modes: {string}?,
count: integer?,
parameters: typepack?,
results: typepack?
}Every fact about one handle at once, as a record whose members are populated according to its kind.
Reach for it when a generator needs several facts about the same handle, or a fact no single accessor exposes, such as a pack's tail or a C array's count. A member that does not apply to the handle's kind is nil.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | typepack | the type or type pack to inspect |
Returns
| Type | Description |
|---|---|
{
kind: string,
sourceKind: string?,
--- Reports the nominal declaration category without expanding a reference.
declarationKind: string?,
name: string?,
value: (string | boolean | integer)?,
element: type?,
members: {type}?,
fields: {{
name: string,
read: type?,
write: type?
}}?,
indexer: type?,
readKey: type?,
readValue: type?,
writeKey: type?,
writeValue: type?,
nominal: boolean?,
head: {type}?,
tail: type?,
modes: {string}?,
count: integer?,
parameters: typepack?,
results: typepack?
} | its descriptor |
elementscomptime function#
The fixed elements of a composite type, or a pack's fixed head.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | typepack | the type or type pack to take apart |
Returns
| Type | Description |
|---|---|
{type} | one handle per element, in declaration order |
errorcomptime function#
Rejects this type application deliberately, with an authored message.
Distinct from an evaluator crash or a timeout: the message is reported at the application, with a bounded comptime call trace. It returns never, so a generator may call it in a branch that has no type to return.
Arguments
| Name | Type | Description |
|---|---|---|
message | string | what to report at the application |
Returns
| Type | Description |
|---|---|
never | nothing; the call does not come back |
fieldscomptime function#
The public fields of a shape or of a nominal record, interface, or struct.
A nominal keeps its declaration identity: reading its fields does not expand it into a structural type. Each descriptor carries a name and whichever of read and write the field has. describe(T).fields reports the same descriptors.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | the shape or nominal type to inspect |
Returns
| Type | Description |
|---|---|
{{
name: string,
read: type?,
write: type?
}} | one descriptor per field, in declaration order |
Raises
| Type | Condition |
|---|---|
string | when the handle is neither a shape nor a nominal declaration |
formatArgumentscomptime function#
local formatArguments: function(format: type): typepackComputes the arguments consumed by a literal Lua format string.
A broad string produces an open any pack. %? is reserved for compiler-lowered formatting APIs.
Arguments
| Name | Type | Description |
|---|---|---|
format | type | the format-string type to inspect |
Returns
| Type | Description |
|---|---|
typepack | the argument pack consumed by its directives |
function_comptime function#
local function_: function(parameters: typepack, results: typepack): typeA function type from its parameter and result packs. Spelled with a trailing underscore because function is a keyword.
Arguments
| Name | Type | Description |
|---|---|---|
parameters | typepack | the parameter pack |
results | typepack | the result pack |
Returns
| Type | Description |
|---|---|
type | the function type |
indexercomptime function#
local indexer: function(
readKey: type?,
readValue: type?,
writeKey: type?,
writeValue: type?
): typeA type indexed by computed keys rather than by named fields.
Each capability is a key/value pair: supply both halves or neither. At least one capability is required, and a read-only or write-only indexer is the point of taking them separately. map is the shorthand for the common case where both sides agree.
Arguments
| Name | Type | Description |
|---|---|---|
readKey | type? | the key type reads accept |
readValue | type? | the value type reads answer |
writeKey | type? | the key type writes accept |
writeValue | type? | the value type writes accept |
Returns
| Type | Description |
|---|---|
type | the indexer type |
Raises
| Type | Condition |
|---|---|
string | when a capability is missing half of its pair, or when neither capability is supplied |
intersectioncomptime function#
local intersection: function(members: {type}): typeThe intersection of its members, as a set, flattened and deduplicated the way union is. An intersection of function types is an overload set.
Arguments
| Name | Type | Description |
|---|---|---|
members | {type} | the member types, in any order |
Returns
| Type | Description |
|---|---|
type | the intersection type |
kindcomptime function#
What a handle is, as a string, which is the usual way to branch on one.
The answer is the handle's own kind, not the builder that made it: map returns "indexer", optional returns "union", pointer returns "ptr", and constof returns "const". A nominal declaration returns "reference", and describe(T).declarationKind says which category it declares.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | typepack | the type or type pack to inspect |
Returns
| Type | Description |
|---|---|
string | the kind, such as |
lengthcomptime function#
base narrowed to the byte lengths from min through max, both included.
Bytes, matching Nupp's own string length: not code points, not graphemes. An embedded NUL counts like any other byte. The base is string or a constrained alias of it.
Arguments
| Name | Type | Description |
|---|---|---|
base | type | the type being narrowed |
min | integer | the shortest admitted length in bytes |
max | integer | the longest admitted length in bytes |
Returns
| Type | Description |
|---|---|
type | the constrained type |
Raises
| Type | Condition |
|---|---|
string | when the base is not a string or the bounds are empty |
literalcomptime function#
The singleton type inhabited by a value.
A number must be an exact integer, since a literal type names a value the checker compares for equality.
Arguments
| Name | Type | Description |
|---|---|---|
value | string | boolean | integer | the inhabitant to lift into type position |
Returns
| Type | Description |
|---|---|
type | the type whose only value is |
Raises
| Type | Condition |
|---|---|
string | when the value is not a string, boolean, or exact integer |
mapcomptime function#
local map: function(readKey: type, readValue: type, writeKey: type?, writeValue: type?): typeAn indexer whose write side mirrors its read side unless stated otherwise.
Equivalent to indexer(readKey, readValue, writeKey or readKey,
writeValue or readValue), and it returns a handle of kind "indexer".
Arguments
| Name | Type | Description |
|---|---|---|
readKey | type | the key type reads accept |
readValue | type | the value type reads answer |
writeKey | type? | the key type writes accept, defaulting to |
writeValue | type? | the value type writes accept, defaulting to |
Returns
| Type | Description |
|---|---|
type | the indexer type |
nonExhaustivecomptime function#
local nonExhaustive: function(): typeThe member that keeps a union open, so a switch over it stays inexhaustive.
No name resolves to this type, so calling for it is the only way to place it in a union. That is what makes a non-exhaustive union a written decision rather than an inferred one. It may be written directly in a type as well as inside a generator.
Returns
| Type | Description |
|---|---|
type | the open member |
Raises
| Type | Condition |
|---|---|
string | when given any argument |
optionalcomptime function#
local optional: function(inner: type): typeinner | nil.
Built as a union, so it is idempotent: optional(optional(T)) and optional(T) are one handle.
Arguments
| Name | Type | Description |
|---|---|---|
inner | type | the type to admit nil alongside |
Returns
| Type | Description |
|---|---|
type | the nilable type |
packcomptime function#
A type pack: a fixed head of slots, then an optional tail standing for however many values remain.
modes align with the head one for one and default to "plain"; the others are "affine", "borrowed", "exclusive", and "pinned".
Arguments
| Name | Type | Description |
|---|---|---|
head | {type} | the fixed slot types, in position order |
tail | type? | the type of the remaining values, or nil for a closed pack |
modes | {string}? | the ownership mode of each fixed slot |
Returns
| Type | Description |
|---|---|
typepack | the pack |
Raises
| Type | Condition |
|---|---|
string | when there are more modes than slots, or a mode is not one of the five |
parameterscomptime function#
local parameters: function(callable: type): typepackThe parameter pack of a function type.
Arguments
| Name | Type | Description |
|---|---|---|
callable | type | the function type to inspect |
Returns
| Type | Description |
|---|---|
typepack | its parameters |
Raises
| Type | Condition |
|---|---|
string | when the handle is not a function type |
pinnedcomptime function#
local pinned: function(inner: type): typeinner pinned to its address, so the collector may not move it.
Arguments
| Name | Type | Description |
|---|---|---|
inner | type | the type to pin |
Returns
| Type | Description |
|---|---|
type | the pinned type |
pointercomptime function#
local pointer: function(inner: type): typeA pointer to inner.
Arguments
| Name | Type | Description |
|---|---|---|
inner | type | the pointee type |
Returns
| Type | Description |
|---|---|
type | the pointer type |
rangecomptime function#
base narrowed to the values from min through max, both included.
A constrained type is a subtype of its base: it goes wherever the base goes. Entering one is admission rather than subtyping, so a value the checker cannot prove is refused and nupp.admit is how the program says it meant it. A literal is decided here and needs nothing written.
The base is integer, int32, uint32, or a constrained alias of one, which narrows further by intersecting. Representation is unchanged: this converts nothing.
Arguments
| Name | Type | Description |
|---|---|---|
base | type | the type being narrowed |
min | integer | the lowest admitted value |
max | integer | the highest admitted value |
Returns
| Type | Description |
|---|---|
type | the constrained type |
Raises
| Type | Condition |
|---|---|
string | when the base is unsupported or the bounds are empty or out of range |
readAtcomptime function#
The type read at a key: the named field when the key is a string literal naming a readable one, and otherwise the indexer's read value.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | the type to project |
key | type | the key to read at |
Returns
| Type | Description |
|---|---|
type | the value type at that key |
Raises
| Type | Condition |
|---|---|
string | when the type has no read capability there, reported as NUPP2420 |
readKeyscomptime function#
Every key a type can be read at: one string literal per readable field, plus the indexer's read key when it has one.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | the shape or indexer to inspect |
Returns
| Type | Description |
|---|---|
{type} | one handle per readable key |
Raises
| Type | Condition |
|---|---|
string | when the handle is neither a shape nor an indexer |
resultscomptime function#
local results: function(callable: type): typepackThe result pack of a function type.
Arguments
| Name | Type | Description |
|---|---|---|
callable | type | the function type to inspect |
Returns
| Type | Description |
|---|---|
typepack | its results |
Raises
| Type | Condition |
|---|---|
string | when the handle is not a function type |
sameNominalcomptime function#
local sameNominal: function(left: type, right: type): booleanWhether two types name the same nominal declaration, ignoring its arguments.
Aliases and ownership wrappers are transparent. Structural types and two different nominal declarations answer false even when their shapes match.
Arguments
| Name | Type | Description |
|---|---|---|
left | type | |
right | type |
Returns
| Type | Description |
|---|---|
boolean |
shapecomptime function#
local shape: function(fields: const{{
name: string,
read: type?,
write: type?
}}, indexer: type?): typeA structural type with named fields, and optionally an indexer for the keys the names do not cover.
Each descriptor needs a non-empty name unique within the shape, and at least one of read and write: a field with only read is readonly, one with only write is writeonly. Field order does not affect identity, so two shapes with the same fields are one handle however they were listed.
Arguments
| Name | Type | Description |
|---|---|---|
fields | const{{
name: string,
read: type?,
write: type?
}} | the field descriptors |
indexer | type? | an indexer handle from |
Returns
| Type | Description |
|---|---|
type | the shape type |
Raises
| Type | Condition |
|---|---|
string | when a name is empty or repeated, when a field declares neither capability, when the indexer did not come from an indexer builder, or when there are more than 1000 fields |
tuplecomptime function#
A fixed-length positional type, one slot per element in the order given.
Arguments
| Name | Type | Description |
|---|---|---|
elements | {type} | the slot types, in position order |
Returns
| Type | Description |
|---|---|
type | the tuple type |
unioncomptime function#
local union: function(members: {type}): typeThe union of its members, as a set.
Nested unions are flattened, duplicates dropped, and the rest ordered canonically, so union{A, union{A, B}} and union{A, B} are one handle.
Arguments
| Name | Type | Description |
|---|---|---|
members | {type} | the member types, in any order |
Returns
| Type | Description |
|---|---|
type | the union type |
writeAtcomptime function#
The type written at a key: the named field when the key is a string literal naming a writable one, and otherwise the indexer's write value.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | the type to project |
key | type | the key to write at |
Returns
| Type | Description |
|---|---|
type | the value type at that key |
Raises
| Type | Condition |
|---|---|
string | when the type has no write capability there, reported as NUPP2420 |
writeKeyscomptime function#
Every key a type can be written at: one string literal per writable field, plus the indexer's write key when it has one.
Arguments
| Name | Type | Description |
|---|---|---|
value | type | the shape or indexer to inspect |
Returns
| Type | Description |
|---|---|
{type} | one handle per writable key |
Raises
| Type | Condition |
|---|---|
string | when the handle is neither a shape nor an indexer |
Values#
anycomptime type#
local any: typeThe dynamic type; compatible with everything, checked at neither end.
booleancomptime type#
local boolean: typetrue or false.
cdatacomptime type#
local cdata: typeAny cdata value.
cstringcomptime type#
local cstring: typeconst char *.
floatcomptime type#
local float: typeAn established binary32 Lua number; widens to number.
int16comptime type#
local int16: typeSigned physical storage; loads as int32.
int32comptime type#
local int32: typeAn established signed 32-bit Lua integer.
int64comptime type#
local int64: typeA boxed 64-bit signed cdata integer, as LuaJIT's 1LL.
int8comptime type#
local int8: typeSigned physical storage; loads as int32.
integercomptime type#
local integer: typeA number known to be integral.
nevercomptime type#
local never: typeThe bottom type; fits everything, nothing fits it.
nil_comptime type#
local nil_: typeThe nil singleton, spelled nil_ because nil is a keyword.
numbercomptime type#
local number: typeA LuaJIT double.
stringcomptime type#
local string: typeA Lua string.
table_comptime type#
local table_: typeAny table shape, compatible with every table type in both directions, spelled table_ because table names the standard library namespace.
threadcomptime type#
local thread: typeA coroutine.
uint16comptime type#
local uint16: typeUnsigned physical storage; loads as uint32.
uint32comptime type#
local uint32: typeAn established unsigned 32-bit Lua integer.
uint64comptime type#
local uint64: typeA boxed 64-bit unsigned cdata integer, as LuaJIT's 1ULL.
uint8comptime type#
local uint8: typeUnsigned physical storage; loads as uint32.
unknowncomptime type#
local unknown: typeThe top type; everything fits it, it fits nothing else.
userdatacomptime type#
local userdata: typeUserdata.
voidptrcomptime type#
local voidptr: typevoid *.