Design decisions

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, with f bound 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 and print/read move onto (plan/DOMAINS.md §1). Why a loop and not a macro or a comptime block: 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/continue are refused (E0501) because an unrolled body has nowhere to jump to; fields is not reserved, so a user fn fields wins. To reverse: delete the fields_loop paths in cpl-check and cpl-ir lowering; nothing else depends on them yet.
  • 28.2 Enums order by variant, then payload. <, sort, max and 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 because ordered() 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 derived Ord. To reverse: drop the Ty::Enum arm of ordered() in cpl-ir/src/lower/expr.rs.
  • 28.3 A variadic parameter is a tuple pack. ..xs: Ts takes the remaining positional arguments as one tuple; Ts must 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 with for 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: Int as a list: a List<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 the E0501 in cpl-ir lower/mod.rs signature lowering and drop pack_args and the pack branch of args in cpl-check.
  • 28.4 f<T>() on a lowercase fn. read<(Int, Int)>() and read<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 because b is lowercase; i < N > (k) with a capitalized N would now parse as a call, a shape no program in the corpus has. f::<T>() still works. This reverses synthesis #2's deletion of read<Int>() (its s2_read_angle case is now ok_s2_read_angle), because design/spec writes read<T>() throughout and a pack type read<(A, B)>() needs a spelling. To reverse: restore the type_like && gate in cpl-syntax parser/expr.rs and drop names_a_type.
  • 28.5 Lazy parameters. fn or_else<T>(a: ?T, lazy d: () -> T) -> T takes, at the call site, an expression e of type T, and passes () => e; the body calls d() when it wants the value. So ??-, assert- and debug-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); lazy only changes what the caller writes, and a lambda is still accepted as is. lazy is a contextual word, so lazy stays a usable name. Rejected: Scala's by-name d: => T (each read re-evaluates, invisibly) and an implicit conversion from T to () -> T for 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: drop Param::lazy in cpl-syntax, the slot match in cpl-check check/call.rs, and thunk_lazy_args in cpl-ir lower/mono.rs.
  • 28.6 fields over an enum. for f in fields(e) on an enum value runs the body over the payload of the variant e is: lowering emits one if per 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: drop variant_payloads in cpl-check check/stmt.rs and the Ty::Enum branch of fields_loop in cpl-ir lower/stmt.rs.
  • 28.7 Derives as library code: std.derive. eq_fields, lt_fields and show_fields are generic fns on fields (28.1) in stdlib/derive.cp (imported, not in the prelude, which is capped at 80 names): the field-by-field Eq, Ord and Show, which an impl can 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 filled eq_fn/lt_fn/show_fn from 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, where cpl_tie costs one line. The default moves onto std.derive once codegen emits a hook only when it is reached. To reverse: delete stdlib/derive.cp.