Is it possible to embed generic structs with go2go?

Hi, I wonder if it is possible to use composition and embed a generic struct (with go2go of course)?

I wan’t to do something like this:

type A[T any] struct {
    a T
}

func (a A[T]) F() T {
    return a.a
}

type B[T any] struct {
    A[T]
}

func main() {
    b := B[string]{A[string]{"Hello"}}
    fmt.Println(b.F())
}

I would get this error message which is referring to the call to “Println” in the main-function: “type B[string] of b does not match A[T] (cannot infer T)”

A non-generic version of this might be this (which works and prints “Hello”):

type A struct {
    a string
}

func (a A) F() string {
    return a.a
}

type B struct {
    A
}

func main() {
    b := B{A{"Hello"}}
    fmt.Println(b.F())
}

I am not very familiar to this new generic aspect in Go, so it might be that I am doing it wrong? Or is it impossible?

Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.