nupp.store

Typed keys and the stores they index.

A key is a phantom-typed identity: Nupp remembers T, the runtime holds an id and an optional name. A store is one table indexed by id behind generic methods, so store:get(key) reads T? and store:set(key, value) accepts T.

Names are opaque strings registered once per runtime state. Ids are allocated in declaration order and identify a key inside this state only; the name is the identity that persists.

Module contents

Constructors

ConstructorDescription
newKeyCreates a key.
newStoreCreates an empty store.

Types

TypeKindDescription
KeyinterfaceA typed identity for one value in a nupp.store.Store.
KeySteptypeThe step of nupp.store.registeredKeys.
StorerecordAn independent typed bag indexed by keys.
StoreSteptypeThe step of Store.entries.

Functions

FunctionKindDescription
findKeyfunctionFinds a registered key by name.
registeredKeysfunctionWalks every registered name, ascending by id.

Constructors#

newKeyconstructor#

function newKey<T>(name: string?): Key<T>

Creates a key.

The type argument comes from the annotation at the declaration. A named key is registered for the runtime state's lifetime; an anonymous key is retained by nothing but its holders.

Type parameters

NameDescription
T

Arguments

NameTypeDescription
namestring?

the opaque name to register, or nil for an anonymous key

Returns

TypeDescription
Key<T>

the key

Raises

  • when the name is empty or already registered, before an id is used

newStoreconstructor#

function newStore(): Store

Creates an empty store.

Returns

TypeDescription
Store

the store

Types#

Keyinterface#

interface Key<T>
    readonly id: integer
    readonly name: string?
end

A typed identity for one value in a nupp.store.Store.

id and name are all a key holds at runtime. _valueType is never present: it mentions T in both positions so that keys are invariant, which is what makes a wrong write through one a checker error rather than a silent widening.

Type parameters

NameDescription
T

Fields

id#
id: integer
name#
name: string?

KeySteptype#

type KeyStep = function(any, integer): (integer, string)

The step of nupp.store.registeredKeys.

Terminating on nil is the generic for's business, so the surface type promises what the loop delivers rather than what the last call returns.

Storerecord#

record Store
    get: function<T>(self: Store, key: Key<T>): T?
    set: function<T>(self: Store, key: Key<T>, value: T): nil
    remove: function<T>(self: Store, key: Key<T>): nil
    clear: function(self: Store): nil
    entries: function(
        borrows self: Store
    ): (function(Store, integer): (integer, string?, string), Store borrows (self), integer)
end

An independent typed bag indexed by keys.

Methods

get#
get: function<T>(self: Store, key: Key<T>): T?

Answers the value held under a key.

Arguments
NameTypeDescription
selfStore
keyKey<T>
Returns
TypeDescription
T?
set#
set: function<T>(self: Store, key: Key<T>, value: T): nil

Stores a non-nil value under a key.

Arguments
NameTypeDescription
selfStore
keyKey<T>
valueT
Returns
TypeDescription
nil
Raises
  • when value is nil; use remove to delete an entry

remove#
remove: function<T>(self: Store, key: Key<T>): nil

Removes the value held under a key.

Arguments
NameTypeDescription
selfStore
keyKey<T>
Returns
TypeDescription
nil
clear#
clear: function(self: Store): nil

Removes every value without touching key registration.

Arguments
NameTypeDescription
selfStore
Returns
TypeDescription
nil
entries#
entries: function(
    borrows self: Store
): (function(Store, integer): (integer, string?, string), Store borrows (self), integer)

Walks every occupied key ascending by id, without its value.

Arguments
NameTypeDescription
borrows selfStore
Returns
TypeDescription
function(Store, integer): (integer, string?, string)
Store borrows (self)
integer

StoreSteptype#

type StoreStep = function(Store, integer): (integer, string?, string)

The step of Store.entries.

Terminating on nil is the generic for's business, so the surface type promises what the loop delivers rather than what the last call returns.

Functions#

findKeyfunction#

function findKey(name: string): unknown

Finds a registered key by name.

The registry knows the name's id and nothing about its type, so the result is unknown and the caller casts to the Key<T> it claims.

Arguments

NameTypeDescription
namestring

the registered name

Returns

TypeDescription
unknown

the key, or nil when the name is not registered

registeredKeysfunction#

function registeredKeys(): KeyStep, any, integer

Walks every registered name, ascending by id.

The walk allocates nothing: the step is one shared function and the control variable is the id it last reported. The registry itself is never handed out, so nothing the loop is given can be used to reach or change it. An anonymous key registers no name and is not walked.

Returns

TypeDescription
KeyStep

the step the loop calls

any

nothing to carry, since the ids are the whole state

integer

the id to resume after, 0 to start