beginSequenceLazy method
- int id
Opens a nested sequence — a fresh id scope (CORELIB_PLAN §4.9) — whose header is held back until the sequence turns out to have content.
MESSAGE_SPEC §2 omits a sequence-typed field whose value equals its declared default, and "not one child was written" is exactly that condition — evaluated per child field, recursively, for free, because the message layer already omits every child equal to its default. A sequence closed with nothing in it therefore emits nothing instead of a two-byte empty frame, and an all-default message becomes the empty byte string.
The predicate is never a byte image of the object, so struct padding and in-memory layout cannot influence it, and a non-zero nested default is handled by the caller's ordinary per-field test.
This is the only way to open a sequence. How it closes decides whether a contentless one survives: endSequence drops it, endSequenceKeep forces the frame out.
There is no depth window: the pending run holds back to the full
nesting bound — the constructor's depth, maxDepth unless the caller
declared less — so a contentless nest is dropped at every legal depth and
the output is canonical everywhere (CORELIB_PLAN §6). Nesting past that
bound throws: SofabError.invalidArgument past a declared depth below
maxDepth (the caller's own promise broken), and
SofabError.invalidMessage past maxDepth itself.
Implementation
void beginSequenceLazy(int id) {
if (_depth >= _pendingSeq.length) _depthExceeded();
if (id < 0 || id > idMax) {
throw const SofabException(
SofabError.invalidArgument,
'field id out of range 0..2^31-1',
);
}
// The run was sized to the nesting bound at construction and the depth
// check above has already refused the one that would overflow it, so there
// is nothing to grow.
_pendingSeq[_nPendingSeq++] = id;
_depth++;
}