How to define an interface that refers to itself?

225 views
Skip to first unread message

awaw...@gmail.com

unread,
Dec 25, 2025, 4:02:21 AM (5 days ago) Dec 25
to golang-nuts
Hi fellow Gophers

I am trying to define an interface that satisfies addition (ultimate goal is to define a field).
However, I could get the following code to compile:


no matter what approach I use (types Field0, Field1, Field2).
The compiler error is of the pattern:

```
./prog.go:29:22: *big.Rat does not satisfy Field0[any] (wrong type for method Add) have Add(*big.Rat, *big.Rat) *big.Rat want Add(any, any) any
```

I took the first approach from this recent post on generics.
If anyone could point to a correct way to make these kinds of self-referencing definitions would be much appreciated.

Thanks

```
package main

import (
"fmt"
"math/big"
)

type Field0[T any] interface {
Add(T, T) T
}

type Field1[T any] interface {
Add(Field1[T], Field1[T]) Field1[T]
}

type Field2 interface {
Add(Field2, Field2) Field2
}

type PolynomialTerm[K Field0[any]] struct {
Coefficient K
Monomial    []byte
}

func main() {
c := big.NewRat(0, 1)

        // This line doesn't compile no matter what Field we choose in PolynomialTerm's definition.
t := PolynomialTerm[*big.Rat]{Coefficient: c}

fmt.Println(t)
}
```

Axel Wagner

unread,
Dec 25, 2025, 4:59:34 AM (5 days ago) Dec 25
to awaw...@gmail.com, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/905e4f95-737f-48cb-bf9b-66e94e31e521n%40googlegroups.com.

awaw...@gmail.com

unread,
Dec 25, 2025, 5:47:29 AM (5 days ago) Dec 25
to golang-nuts
Thank you Axel for this educative solution, much appreciated.

Robert Engels

unread,
Dec 27, 2025, 11:49:45 AM (2 days ago) Dec 27
to Axel Wagner, awaw...@gmail.com, golang-nuts

Field1 and Field2 are unused in your example. What is the purpose?

awaw...@gmail.com

unread,
Dec 28, 2025, 3:02:12 AM (yesterday) Dec 28
to golang-nuts
Hi fellow Gophers

Unfortunately, I have another generics question that is truly driving me nuts.
Why does the below snippet not compile:


with the error message:

```
./prog.go:27:9: cannot use &ASet{} (value of type *ASet) as Set[T] value in return statement: *ASet does not implement Set[T] (wrong type for method NewElement) have NewElement() A want NewElement() T
```

I'd assume `*ASet` obviously satisfy the `Set` interface.

Many thanks in advance.

p.s. Robert, the Field1, Field2, ... are just different failed approaches I tried. Luckily, Axel showed me the solution. I thought everything went well until I hit this problem...

```
package main

import "fmt"

type A struct{ F int }
type ASet struct{}

func (s *ASet) NewElement() A {
return A{F: 5}
}

type B struct{ F float64 }
type BSet struct{}

func (s *BSet) NewElement() B {
return B{F: 3.14}
}

type Set[T A | B] interface {
NewElement() T
}

// This function definition does not work. Why?!
func NewSet[T A]() Set[T] {
return &ASet{}
}

func main() {
s := NewSet[A]()
fmt.Println(s)
}
```

Axel Wagner

unread,
Dec 28, 2025, 5:40:36 AM (yesterday) Dec 28
to awaw...@gmail.com, golang-nuts
On Sun, 28 Dec 2025 at 09:02, awaw...@gmail.com <awaw...@gmail.com> wrote:
Hi fellow Gophers

Unfortunately, I have another generics question that is truly driving me nuts.
Why does the below snippet not compile:


As of now, the language does not imply that T==A, even if the type set of T only has one type in it. You can work around that by converting to an interface and using a type-assertion:

It's a bit unfortunate, but them's the breaks right now.
 

awaw...@gmail.com

unread,
Dec 28, 2025, 6:46:08 AM (yesterday) Dec 28
to golang-nuts
Thank you Axel immensely for your explanation and working example. I really benefited from them a lot.
With this, I can now do polymorphism on the generic type.
I have to say I am pretty impressed by the power and versatility of Go's generics type system.

BVK Chaitanya

unread,
Dec 28, 2025, 12:16:06 PM (yesterday) Dec 28
to awaw...@gmail.com, golang-nuts

It seem to work with single type parameter as well:

```
type PolynomialTerm[K Field0[K]] struct {
  Coefficient K
  Monomial    []byte
}
```

Reply all
Reply to author
Forward
0 new messages