Add optional fields and variable-length field support#7
Draft
Add optional fields and variable-length field support#7
Conversation
Agent-Logs-Url: https://github.com/rotu/structview/sessions/3465639d-8c89-4269-94e2-6dbf1256113f Co-authored-by: rotu <119948+rotu@users.noreply.github.com>
…in typedArray Agent-Logs-Url: https://github.com/rotu/structview/sessions/3465639d-8c89-4269-94e2-6dbf1256113f Co-authored-by: rotu <119948+rotu@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Propose design for optional and variably sized fields in API
Add optional fields and variable-length field support
Apr 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The library had no way to express fields that are conditionally absent or whose size is determined at runtime. This adds three composable, declarative primitives that fit naturally alongside the existing field factories.
optional(descriptor, presence)— new infields.tsWraps any field descriptor to return
T | nullwhen the field is absent. Two strategies:optional(u16(0), { sentinel: 0xffff })—nullwhen stored value equals sentinel (Object.is); writingnullstores the sentinel.optional(u32(4), (dv) => (dv.getUint8(0) & 0x01) !== 0)—nullwhen predicate returns false; writingnullis a no-op.Writability of the result mirrors the wrapped descriptor: writable ↔ input has a setter, read-only ↔ input is read-only (e.g.
substruct,typedArray). Enforced via TypeScript overloads.Dynamic-length
string()overload —fields.tsstring(offset, { length: propertyName | (dv) => number })— read-only string whose byte length is resolved at access time from another field or an arbitrary DataView function.Write support is intentionally omitted: writing a variable-length string requires coordinating the length field, which is better expressed with
fromDataView.Function-based
lengthintypedArray()—fields.tsThe
lengthoption now additionally accepts(dv: DataView) => number, complementing the existingnumber | string | undefinedforms. Fully backward compatible.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
dl.deno.land/usr/bin/curl curl -s REDACTED(dns block)If you need me to access, download, or install something from one of these locations, you can either: