ADR 0043: Struct embedding
Status: Accepted (2026-10-25). Implements the [planned] item of the spec (language.md §3.3) and removes it from the documented limitations (overview §5). Not breaking: it adds syntax that is a parse error today. It is listed with the decisions because its spelling must be fixed before people work around it.
Context
Section titled “Context”Lyth structs cannot embed. Promotion through embedded fields of Go structs
already works, because member lookup on Go types is delegated to go/types
(goMember in internal/check/member.go). But a Lyth struct cannot embed
anything, and the review found that this blocks or bloats common backend
code.
- gRPC. Generated server interfaces contain the unexported method
mustEmbedUnimplemented…Server(), which only embedding can provide. A Lyth server does not compile against defaultprotoc-gen-go-grpcoutput. The workarounds are the generator flagrequire_unimplemented_servers=false, which gives up forward compatibility, or a hand-written Go adapter inside the generated package. - JWT. golang-jwt custom claims embed
jwt.RegisteredClaims. Without embedding, six forwarding methods are needed, andexp/iatnest under a field instead of sitting at the top level of the token. Other services then cannot read them. - Middleware. A status-recording
http.ResponseWriterforwards every method by hand. - Shared fields. Embedded mutexes (
sync.Mutex), testify’smock.Mockandsuite.Suite, and shared fields flattened into JSON (Timestampsin every model) all need embedding. - Partial test fakes. Go embeds the interface in the fake and overrides
one method. In the review, a fake implemented all 10 methods of sqlc’s
Querier, about 110 lines. - Diagnostics. Go’s embedding syntax gives raw parse errors today
(
expected ':' after field name). In one gRPC server it cascaded to 14 errors, although the overview promises a specific diagnostic for each limitation.
Decision
Section titled “Decision”-
Syntax. Go’s spelling: a type alone on a line of the struct body embeds it.
struct statusRecorder {http.ResponseWriterstatus: int}func *statusRecorder.WriteHeader(code: int) {self.status = codeself.ResponseWriter.WriteHeader(code)}struct Claims {jwt.RegisteredClaimsUserID: string}struct server {pb.UnimplementedGreeterServerstore: Store}The embedded type is a type name, optionally qualified, instantiated, or behind
*:T,*T,pkg.T,Page[int]. Embedding a type parameter is an error, as in Go. -
The field. The field’s name is the type’s name without package or
*(ResponseWriter,RegisteredClaims). It is exported when that name is exported (Go’s rule). -
Construction. Embedded fields are constructed by that name (
statusRecorder(ResponseWriter = w)) and take part in positional construction in declaration order. -
Promotion. It follows Go exactly: promoted fields, promoted methods, method sets, depth, and ambiguity. Lyth delegates it to
go/typesover the shadow package, as it does for Go structs. Promoted Lyth members keep their Lyth types (nullability,throws). A method declared on the outer type hides a promoted one of the same name, so the example’sWriteHeaderoverrides the writer’s. Interface satisfaction counts promoted methods, which is what makes the gRPC and jwt cases work. -
Access. Promoted members are reached through a value, or through
selfinside methods (self.Header(),self.Lock(); ADR 0038). -
Nullability.
- Non-null embedded types must be given at construction. An embedded
pointer, interface, function, or channel type is non-null, so it has no
zero value. Embedded value types are zeroable as usual (
sync.Mutex,RegisteredClaims). - A nullable embedded type is allowed (
todo.Store?). It exists for partial fakes. Its promoted members still make the outer type satisfy interfaces (Go’s method set), so a call through the interface reaches a nil embedded value and panics, which is the Go behaviour fakes rely on. A direct access in Lyth code (fake.Delete()) needs the field checked first: E0401, “Delete is promoted through the nullable field Store”.
- Non-null embedded types must be given at construction. An embedded
pointer, interface, function, or channel type is non-null, so it has no
zero value. Embedded value types are zeroable as usual (
-
JSON. Embedded fields get no automatic
jsontag, soencoding/jsonflattens embedded structs as in Go. An explicit tag on the embedding line nests it:Timestampsfollowed by the tagjson:"ts"puts its fields under"ts". -
Sealed interfaces. Embedding a member of a sealed interface does not make the outer struct a member. Membership is declared (ADR 0025). Converting the outer struct to the sealed interface is E0201, which says so.
Alternatives
Section titled “Alternatives”- A keyword (
embed http.ResponseWriter,...Base). It is more visible, but it is not Go’s spelling, and Go readers already know a lone type line. - Delegation with explicit forwarding (Kotlin’s
by). It gives no field and no Go method-set promotion, so the gRPC and jwt cases stay impossible, and it lowers to many generated methods. - Embedding only Go types. It covers gRPC, jwt, and mutexes, but not shared Lyth models or partial fakes, and it leaves an odd rule to teach.
Consequences
Section titled “Consequences”- What works. gRPC services work with default code generation; jwt claims,
ResponseWriterwrappers, embedded mutexes, testify suites and mocks, partial fakes, and flattened JSON read as in Go. - Nullable embedding. It is the one place where a promoted call can panic
on nil without
!!(through an interface), and only on a field the author declaredT?. The spec says so. - The limitation goes. The overview’s limitation list and the guide’s “Not supported yet” lose struct embedding.
Migration
Section titled “Migration”None: embedding lines are parse errors today.
Implementation
Section titled “Implementation”- Parser. A
FieldDeclalternative for a lone type line. The E0003 recovery forpkg.Typelines becomes the real parse. - Types and checker.
types.FieldgainsEmbedded. The shadowgo/typesstruct embeds the field, soLookupFieldOrMethodresolves promotion. The checker maps a promoted Lyth member back to its Lyth declaration for nullability andthrows, adds the nullable-embedding rule, the sealed-membership check, and the tag rule. - Lowering. The field is emitted without a name.
- Tests.
run/embedding:ResponseWriterwrapper, mutex, shared fields, override, JSON flattening and a tagged embedding, a generic embedded type, and a partial fake whose null embedded interface panics when called through the interface.check/embedding_neg: type parameter, pointer to an interface, a non-name type, ambiguous selector, nullable promotion, a promoted pointer method on a value receiver, sealed membership, construction, and Go’sName Typefield spelling (E0008 with a fix).TestEmbeddingAcrossPackages: a Go package shaped likeprotoc-gen-go-grpcoutput (an interface with the unexportedmustEmbedUnimplementedGreeterServer), and a Lyth struct of another package whose promoted members keep their Lyth types. A generated gRPC service needsprotoc, which the test suite does not run.