String keys and Record
On this page

String keys and Record

A union of string literal types is a key type. It names a closed set of values such as channels or modes.

Key types

The compiler sorts members by UTF-16 code units and assigns a u32 id to each member. A single string literal type is also a key type. Equal member sets give the same ids. The GPU receives the id on every target.

A key supports equality and switch over its members. Arithmetic, ordering and conversion to a number are refused. A string literal becomes a member where the key type is expected. Open string stays refused.

Record

Record<K, V> holds one value of V per member of K, as a fixed array in id order. by[c], by.r and by["r"] select its elements. An object literal must provide every member, with no extra property. Another key type, a number and .length are refused.

"use typeshade";
type Channel = "r" | "g" | "b";
export function gain(c: Channel, by: Record<Channel, f32>): f32 {
return by[c];
}

Numeric keys

An enum or an integer-literal union can key a Record when its distinct values are exactly 0 through N-1. Each member indexes by its value. A gap, a negative value or a noninteger is refused with a sentence naming the values. Open Record<u32, V> is refused.

Host calls

A key parameter or result is the string on the host. A Record parameter or result is an object with every member. An unknown string, a missing property or an extra property produces a TypeError.

host.ts
import { gain } from './gain.shade.ts';
const value = gain('g', { r: 1.2, g: 1, b: 0.8 });

In entry bindings and runtime-sized kernel arrays, keys are numeric ids and records are arrays in id order. Proposal 0081 keeps this difference open as D1.

Map and Set

A kernel accepts Map<Channel, V> and Set<Channel>. The host passes string keys. Before the loops, the call converts them to ids. Afterward, it converts ids back to strings and preserves insertion order. An unknown string produces a TypeError. Open Map<string, V> and Set<string> are refused.

string-keys-record calls a Record-indexed kernel on WebGPU, WebGL2 and the CPU. Control flow describes collection loops and their supported tiers.

Edit this page Report a problem