Skip to content

v0.7.0: Generics support

Latest

Choose a tag to compare

@gobwas gobwas released this 28 Oct 13:56
gtrace: support type parameters on traced structs

A struct may be declared as `type Trace[T any] struct { ... }`: the
generated hook methods, Compose and isZero become methods of Trace[T],
the context helpers and shortcuts become generic functions, and the
context key type takes the same parameters so two instantiations in one
context do not collide. An instantiated generic trace, from this package
or another, composes as a field like any type with a Compose method.

Discovery takes the type spec whole instead of walking into it: the
type parameter list would be visited before the struct body, and
leaving a visited node resets the pending item. A spec inside a grouped
declaration carries its own doc, which is read the same way.

Stubs of a generic trace return their zero values inline, since no
package-level stub variable or function can name a type parameter.

Fixtures test_generic.go and test_generic_tags.go cover hooks, compose,
context, shortcuts, a composed generic field and the stub build; go.mod
moves to 1.18 for them.