Skip to content

Go: slices of structs and keyed slice literals ​

Status: accepted · Date: 2026-11-30

Step 4 of the Go plan lists keyed elements, slices of structs and xs == nil among the slice expressions still to do. This record adds the first two and explains why the third waits.

The decision ​

  • Slices of structs. []P for a struct type P of the subset is now a type of the subset (Pure.elemTypeOf). It covers parameters, locals, []P{P{…}, …}, append, len and ps[i].f. The output is List P. For ps[i], the output's default element is the struct with each field's zero value (Pure.elemZero). The output reads it only when the index is out of range, and then the statement panics first.
  • Keyed elements. []T{2: a, b, 5: c} puts each element at its index: the key's, or one after the previous element's. The zero value of T fills the gaps (Go specification, "Composite literals"). Eval.slicePositions computes the indices and Eval.placeAt lays the values out. The interpreter and the translator share both. The output is the list with the zeros written out.

Refused ​

  • Keys that are not integer literals, or that do not grow. Go evaluates the elements in source order, and the output evaluates its list in index order, so the two orders agree only when the keys grow. Go accepts constant expressions as keys, and any order of distinct keys.
  • Keys above 2^20, as for make.
  • A keyed literal of structs. The gaps would need a struct's zero value in the interpreter as well, which is left for later.
  • Elements without their type, such as []P1. Slices of slices came later (decision).
  • xs == nil and xs != nil. The subset models a slice as the list of its elements, so nil and an empty slice such as []int{} or make([]int, 0) have the same value. Go tells them apart. Supporting the comparison needs a slice value that records whether it is nil. That changes the interpreter's Val.slice, append (which turns nil into a non-nil slice), and the output type, which would become Option (List T) or a structure. It is left for a later step.

This page is docs/decisions/2026-11-30-go-slice-literals.md at commit 5f1d031.