Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

OptionWhy not / why chosen
Structural assignability for any struct with all base fieldsRejected. 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 : BRejected 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 explicitlyRejected. 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 shapeRejected. 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 accessChosen. 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 embed field per struct. Its type must be a struct (not an enum, primitive, collection, type parameter, or function).
  • D is assignable to B iff D’s embed field has exactly type B. This is deliberately one step: if C embeds D and D embeds B, C is not assignable to B.
  • No covariance or inference through containers: List<D> is never assignable to List<B>, and a field of type Option<D> is not Option<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 B instead of using a bare T.
  • 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 as users.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 at users.table_info.columns and the own field at users.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_info both export as tableInfo/TableInfo, so either would collide with a literal column on the host side. metadata or table_meta are 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 embed is still legal: embed: Str is a field, while embed x: T is 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:

TargetSourceEmitted
TypeScriptfrom(select(), users)from(select(), users.tableInfo)
Pythonfrom(select(), users)from_(select_(), users.table_info)
Gofrom(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.