https://go.dev/play/p/KV75rDOwIn2

On Thu, 25 Dec 2025 at 10:03, [email protected] <[email protected]> wrote:

> Hi fellow Gophers
>
> I am trying to define an interface that satisfies addition (ultimate goal
> is to define a field <https://en.wikipedia.org/wiki/Field_(mathematics)>).
> However, I could get the following code to compile:
>
> https://go.dev/play/p/zIwo1KW_Un0
>
> 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
> <https://go.dev/blog/generic-interfaces> 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)
> }
> ```
>
> --
> 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 [email protected].
> To view this discussion visit
> https://groups.google.com/d/msgid/golang-nuts/905e4f95-737f-48cb-bf9b-66e94e31e521n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/905e4f95-737f-48cb-bf9b-66e94e31e521n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfFiayZh_3RcbU0iCTsJ3AFNGeykV4MqRKKm%2B%3D2%2BbymBPw%40mail.gmail.com.

Reply via email to