ADR 0047: The Go constructs Lyth lacked
Status: Accepted (2026-10-27). Amends ADR 0031
(for loops, if, groups) and ADR 0012.
Context
Section titled “Context”Porting real Go packages to Lyth (encoding/csv, text/tabwriter, flag, container/list, container/heap, container/ring) showed where Lyth was not yet “all of Go”: the ports passed Go’s own tests, but each had to work around a Go construct that Lyth did not have, and several workarounds were worse than the Go they replaced.
- Three-part
for. tabwriter’sformatadvances its loop variable in the body, heap’sInitcounts down. Aswhileloops with the increment at the end, acontinueskips the increment: the loop hangs, a bug Go’sfor init; cond; postcannot have. const ( ... )withiota. tabwriter’s flags are1 << iotaof typeuint, flag’s error handling is a typediotasequence whose Go names must stayContinueOnError, not an enum’sErrorHandlingContinueOnError. Written one by one, the values are typed by hand and the group’s doc comment, whichgo docshows, is lost.- Named results. tabwriter’s
Writereports the bytes consumed before a recovered panic; the port needed a second function and a pointer.go docof every port lost the names ((n int, err error)) that document Go APIs. - Type-set constraints.
[T int | float64]andinterface Number { ~int | ~float64 }are how generic numeric code is written since Go 1.18. - Opting out of automatic json keys. A ported API must keep encoding
Valueas"Value"; the only way was an explicit tag per field.
ADR 0031’s principle settles each case: Go’s spelling for what Go has, and
Lyth syntax only where it improves on Go. None of these has a Lyth spelling
that improves on Go’s. ADR 0031 kept for for iteration because for cond { } would be ambiguous with a membership test (for x in xs); a three-part
header, with its two semicolons, is not.
Decision
Section titled “Decision”- Three-part
for, as in Go:for i := 0; i < n; i++ { }. The init and post are simple statements (i := 0,i, j := 0, n - 1,i++,i += 2,i, j = i + 1, j - 1); any part may be empty except thatfor ; ; { }is writtenwhile true { }. The variable is a mutable local of each iteration, as in Go 1.22 and later.for i in 0..<nstays the way to count by one, and both lower to Go’s loop. - Groups, as in Go:
const ( ... )at package level and in functions, withiotaand implicit repetition (an entry with only a name repeats the previous expression and type), andvar ( ... )at package level. Entries use Lyth’sname: Type:A = 1,A: uint = 1 << iota,B,Default: Config = Config(),cache: map[string]int. A comment above the group is its doc.iotais the index of the entry in the group (0 outside a group), as in Go.enumstays the declaration of enumerations (names on the wire, exhaustivewhen); groups are for flags, sizes, and Go-compatible names. - Named results:
func split(n: int): (q: int, r: int), and withthrows,func *Writer.Write(buf: []byte): (n: int) throws. The names are locals declared with their zero values (so their types need one, E0405 otherwise), visible todeferblocks, which may assign them. Areturnwith values assigns and returns them; a barereturnreturns them as they are (only in a function with named results). They lower to Go’s named results; athrowsfunction’s error result is named too. - Type sets in constraints and interfaces, as in Go:
[T: ~int | ~float64],interface Number { ~int | ~int64 | ~float64 }. An interface with type terms is a constraint only. json:""keeps encoding/json’s default key (the field name): an empty tag value is the explicit opt-out of the automatic key, and the Go field is left untagged, as the ported Go type was; a tag with options and no name (json:",omitempty") keeps the automatic name as before.ifwith an init statement, as in Go:if v, ok := m[k]; ok { }. The names are in scope in every branch of theif.- Complex numbers, as in Go:
complex64,complex128, imaginary literals (2i), andcomplex,real,imag. - Generic type aliases, as in Go 1.24:
type Set[T: comparable] = map[T]bool. - Named arguments and defaults on generic functions. Type arguments are
inferred from the arguments given; a default whose type is a type
parameter must be a constant, and takes the type the call gives
T. - Parameter names in function types:
(name: string, depth: int) -> bool, kept in Go’sfunc(name string, depth int) bool. - External test packages (
package foo_testin a_test.lythfile), as in Go. lyth run file.lythbuilds that file alone, asgo run file.godoes, also inside a module.
Kept as they are:
for cond { }andfor { }staywhile cond { }andwhile true { }(ADR 0031: ambiguous withfor x in xs).rangestays rejected (ADR 0031).- Anonymous struct types other than
struct{}stay unsupported: a struct declared in the function (struct tc { in: string }) is one line and gives table tests a name that error messages use. T?on a type parameter stays unsupported. Go’s only representation that works for every instantiation is a box (*T), while Lyth’s*User?is a nilable*Userwith no box; aT?instantiated with a pointer type would need a conversion at every call. Generic code returns(T, bool), as Go code does.- A package is one language. Lyth files and hand-written Go non-test files do not mix in one package: each side would need the other’s declarations before its own are checked. Packages of either language import each other, so code moves to Lyth a package at a time.
- No
whenover errors. A typedcatchchain is the construct that matches errors through wrapping; awhenoferrors.Isconditions covers sentinel values.
Consequences
Section titled “Consequences”- Code ported from Go keeps its loops, flag groups, and API documentation.
- The diagnostics that rejected three-part loops, const and var groups, and
named results (E0008 with edits, added in 0.5.0) are removed: the forms
are valid.
lyth fixno longer rewrites them; code it rewrote stays valid. - Two ways to count by one exist (
for i in 0..<nand the three-part form), as in Go (for i := range nand the three-part form). - Generated Go for these constructs is Go’s own construct, unchanged.
Implementation
Section titled “Implementation”- Parser:
ForStmtwithInit,Cond,Post;ConstDecl/PropDeclgroups (Groupwith its doc and the implicit repetition); named results inResultType.Names; union and~Tterms in constraint and interface type expressions. - Checker:
iotain constant expressions, repetition of the previous entry; named results as locals of the function, barereturn; type sets through go/types unions, so operators on type parameters follow Go. - Lowering: Go’s
for, groupedGenDecls with implicit specs, namedFieldListresults, union constraint expressions;encoding/jsontags left as written when empty. - Tests:
run/goforms,check/goforms_neg,gen/goforms, and for the build,TestExternalTestPackage.