28. Core mechanisms for a library-only stdlib (0.3, §1)
- 28.1 Reflection is a loop:
for f in fields(x). The body runs once per field of a struct, record or tuple, in declaration order, withfbound to that field at its own type;fields(a, b)walks values of one type in step, binding a tuple. It is unrolled in lowering, so the interpreter and the C++ see ordinary code and no new IR, and a generic body (fn show_all<T>(x: T)) is checked per instantiation like any other generic body. It is the base the derives, variadic packs andprint/readmove onto (plan/DOMAINS.md§1). Why a loop and not a macro or acomptimeblock: it is the one construct a contestant already reads as "for each", it needs no new keyword, and it keeps generated code visible to the trace and the formatter.break/continueare refused (E0501) because an unrolled body has nowhere to jump to;fieldsis not reserved, so a userfn fieldswins. To reverse: delete thefields_looppaths incpl-checkandcpl-irlowering; nothing else depends on them yet.
- 28.2 Enums order by variant, then payload.
<,sort,maxand the rest work on an enum whose payloads are all ordered: variants compare in declaration order, then field by field, the order the interpreter's values and the C++cpl_tie()already had. Lowering refused it (E0309) only becauseordered()had no enum case, which made the sweep-line idiom (enum Event: Open, Query, Close, then sort) need a hand-written key. Declaration order is the order a contestant writes events in, the same rule as struct fields and as the 0.3 spec's derivedOrd. To reverse: drop theTy::Enumarm ofordered()incpl-ir/src/lower/expr.rs.
- 28.3 A variadic parameter is a tuple pack.
..xs: Tstakes the remaining positional arguments as one tuple;Tsmust be a type parameter, bound per call to(A, B, ..)(the empty tuple for none), and a bound on it (Ts: Show) is a bound on the tuple, which holds when every element meets it. The body walks the pack withfor x in fields(xs)(28.1), so variadics add no new loop, type form or IR: lowering packs the arguments into a tuple and instantiates the generic as usual. Why not the spec's<Ts...>(xs: Ts...): the..on the parameter already marks it, and a second marker on the type parameter says nothing the parameter does not. Why not homogeneous..xs: Intas a list: aList<Int>parameter with[a, b, c]already says that, at one more pair of brackets, and two meanings for..would be a rule to remember; it is refused with that fix. To reverse: restore theE0501incpl-irlower/mod.rssignature lowering and droppack_argsand the pack branch ofargsincpl-check.
- 28.4
f<T>()on a lowercase fn.read<(Int, Int)>()andread<Int>()parse as type arguments, as the spec writes them, when every argument is spelled as only a type can be (a capitalized name, or a tuple, array, optional or record of them) and(follows.a < b > (c)stays a comparison becausebis lowercase;i < N > (k)with a capitalizedNwould now parse as a call, a shape no program in the corpus has.f::<T>()still works. This reverses synthesis #2's deletion ofread<Int>()(itss2_read_anglecase is nowok_s2_read_angle), becausedesign/specwritesread<T>()throughout and a pack typeread<(A, B)>()needs a spelling. To reverse: restore thetype_like &&gate incpl-syntaxparser/expr.rsand dropnames_a_type.
- 28.5 Lazy parameters.
fn or_else<T>(a: ?T, lazy d: () -> T) -> Ttakes, at the call site, an expressioneof typeT, and passes() => e; the body callsd()when it wants the value. So??-,assert- anddebug-shaped fns can be library code that evaluates its argument zero or one time, and the reader of the body sees each evaluation as a call. The declared type stays the function type (one meaning per spelling);lazyonly changes what the caller writes, and a lambda is still accepted as is.lazyis a contextual word, solazystays a usable name. Rejected: Scala's by-named: => T(each read re-evaluates, invisibly) and an implicit conversion fromTto() -> Tfor every fn-typed parameter (the call site of a non-lazy fn would stop saying when its argument runs). Free fns and generic fns only so far; methods next. To reverse: dropParam::lazyincpl-syntax, theslotmatch incpl-checkcheck/call.rs, andthunk_lazy_argsincpl-irlower/mono.rs.
- 28.6
fieldsover an enum.for f in fields(e)on an enum value runs the body over the payload of the varianteis: lowering emits oneifper variant, each unrolled over that variant's fields, and the checker checks the body against every variant's field types.fields(a, b)runs only when both are the same variant. One mechanism serves structs and enums; what derives still need is the variant's index and name (Ord,Hash,Show), next. To reverse: dropvariant_payloadsincpl-checkcheck/stmt.rsand theTy::Enumbranch offields_loopincpl-irlower/stmt.rs.
- 28.7 Derives as library code:
std.derive.eq_fields,lt_fieldsandshow_fieldsare generic fns onfields(28.1) instdlib/derive.cp(imported, not in the prelude, which is capped at 80 names): the field-by-fieldEq,OrdandShow, which animplcan call (fn lt(a, b) = lt_fields(b, a)is the reverse order). The compiler's own structural derive stays the default for now: wiring every struct without an impl to these fns (lowering filledeq_fn/lt_fn/show_fnfrom them) gave the same output but grew emitted C++ past the scoreboard's 5% gate on many ports (ucup5s02h 28210 -> 44772 B, cf2268c 15903 -> 28221 B), because each hook is emitted whether or not it is used, wherecpl_tiecosts one line. The default moves ontostd.deriveonce codegen emits a hook only when it is reached. To reverse: deletestdlib/derive.cp.