Design note: embedded fields and one-step widening
Status: implemented on feature/struct-embed-subtyping (LT 0.5.x line).
Problem
Generated table declarations need both views of the same value:
struct TableInfo {
name: Str
schema: Option<Str>
alt: Option<Str>
columns: List<ColumnInfo>
}
struct UsersTable {
embed table_info: TableInfo
id: IntColumn
name: StrColumn
}
fn from(s: Select, t: TableInfo) -> Select
from(select(), users) // users : UsersTable must be accepted as TableInfo
users.name // must be the column field, not TableInfo.name
Nominal LT structs cannot be passed to a different nominal parameter, and there is no inheritance, structural record, interface, or type alias. Adding implicit structural typing would weaken the one property the three emitters rely on: every value has exactly one host record type.
Options considered
| Option | Why not / why chosen |
|---|---|
| Structural assignability for any struct with all base fields | Rejected. It silently makes Wrapper { name: Str } a TableInfo, weakens nominal typing everywhere, and is impossible to map to Go structs without an interface or conversion. |
Full struct inheritance struct D : B | Rejected for this change. Go has no inheritance; emitting D as a distinct struct would need either an interface (dynamic dispatch) or a generated converter. It also creates field-promotion/shadowing questions for arbitrary column names. |
User-declared interface / trait implemented explicitly | Rejected. Go methods would be dynamic dispatch, and field access through a protocol still needs either methods or generated monomorphisation. Both are larger changes than the downstream need. |
| Emitter-only projection based on field shape | Rejected. Without a source marker, the checker cannot distinguish an intentional wrapper from an accidental one, so the same source would be accepted by one emitter heuristic and rejected by another. |
Explicit embed field + one-step widening lowered to field access | Chosen. The source states the relation; the checker enforces it nominally; all emitters render exactly users.table_info. No inheritance, interface, dynamic dispatch, or runtime check. |
Semantics
A field may be marked once:
struct UsersTable {
embed table_info: TableInfo
id: IntColumn
name: StrColumn
}
Rules:
- At most one
embedfield per struct. Its type must be a struct (not an enum, primitive, collection, type parameter, or function). Dis assignable toBiffD’s embed field has exactly typeB. This is deliberately one step: ifCembedsDandDembedsB,Cis not assignable toB.- No covariance or inference through containers:
List<D>is never assignable toList<B>, and a field of typeOption<D>is notOption<B>. - Widening into an unresolved inference variable is rejected, so the lowering
pass never has to replay generic inference to decide where the projection
goes. Annotate the parameter as
Binstead of using a bareT. - The relation is used by the ordinary assignability check (arguments, annotated bindings, returns, struct fields, list/dict literals). It is not used by equality assertions and never changes runtime equality.
- The embed field is a normal field: it is constructed with a named
argument, it appears in
Eq/Ord/Display, and it is read asusers.table_info. There is no field promotion.
Collision rule for arbitrary column names
The embedded field has an author-chosen snake_case name, exactly like every other field. Duplicate names in one struct are already a compile error, so:
- a derived column may be named
name,columns,schema,table_name, etc. without colliding with metadata, because metadata lives atusers.table_info.columnsand the own field atusers.columns; - if a database column is literally
table_info, the generator must pick a different embed field name. Pick a word that differs after the host naming rules, not a variation of the same word:table_info_and_table_infoboth export astableInfo/TableInfo, so either would collide with a literal column on the host side.metadataortable_metaare safe. It has the complete column set, and there are infinitely many valid snake_case names, so a fresh name always exists; - a field literally named
embedis still legal:embed: Stris a field, whileembed x: Tis the marker. The parser distinguishes them by lookahead.
This is why arbitrary database column names are supported: the embed marker,
not the field name, carries the widening relation. The only requirement is
that the embed field name is unique among that struct’s fields, which the
codegen controls.
Emission
The IR contains Upcast { value, field, ty }; lowering inserts it only where
the checker accepted D for B. Emitters render it as field access:
| Target | Source | Emitted |
|---|---|---|
| TypeScript | from(select(), users) | from(select(), users.tableInfo) |
| Python | from(select(), users) | from_(select_(), users.table_info) |
| Go | from(select(), users) | From(Select_(), users.TableInfo) |
The embed field itself is emitted as an ordinary record member in all three
targets; Go does not use anonymous embedding. Therefore there is no
interface, no dynamic dispatch, no type assertion, and go vet can see the
concrete field access. Eq/Ord/Display are derived from the ordinary field
list, with the embedded member first if it is declared first.
Rejected detail: Go embedding
Using Go’s anonymous embedding (type UsersTable struct { TableInfo; ... })
would give the projection for free, but it also promotes TableInfo fields
into the outer field set and creates shadowing rules that differ from
TypeScript/Python. The chosen named-field form keeps one rule across targets:
users.name is an own field or an error, never a promoted base field.