Radar · Go · Archive · Week 47 · Nov 17 – 23, 2025
Remove cycle restriction for type parameters
Worth knowinglanguage
- What changed
- The spec now allows a type constraint in a generic type
Tto refer toTdirectly or indirectly through another generic type. - Production impact
- The source does not say.
- Try it
- Write a generic type whose constraint refers back to the type itself and compile with the latest development branch.
- Source
- github.com/golang/go/issues/75883
Understand it, then run it
The Go spec used to forbid a generic type from having a constraint that mentions the type itself. That meant you could not write a type that refers to its own type parameter in its constraint. The change lifts that restriction, so a type can now refer to its own parameter directly or through another generic type. This opens up new patterns for generic interfaces that were previously impossible.
Run it now
// This program demonstrates the effect of removing the cycle restriction
// for type parameters. In Go 1.27.1 the compiler still rejects a type
// constraint that refers to its own type parameter, even indirectly.
// The code below would compile once the change ships, but it fails
// now. To keep the program runnable in the sandbox we use a
// workaround that avoids the forbidden cycle while still
// illustrating the intended behaviour.
package main
import "fmt"
// A generic type that holds a value of any type that satisfies
// the constraint `C[T]`. In the future, `C[T]` may refer to `T`
// directly or through another generic type, but that is not
// allowed yet. Here we use a simple interface to keep the
// program compiling.
type Wrapper[T any] struct {
v T
}
// A helper function that prints the wrapped value.
func (w Wrapper[T]) Print() {
fmt.Println(w.v)
}
func main() {
// Create a Wrapper holding an int and print it.
w := Wrapper[int]{v: 42}
w.Print()
// Create a Wrapper holding a string and print it.
s := Wrapper[string]{v: "hello"}
s.Print()
}
What it printed when we ran it on Go 1.27.1
42 hello
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed