Hi everyone,

I am trying to figure out if there's a good way to build a helper that can
be used against different sets of nested types that have a common structure.

So, for example, say there are two libraries, `lib1` and `lib2` and both
define types Foo, Bar, and Baz with:

type Foo struct { ... }
type Bar struct { ... }
func (b *Bar) GetFoo() *Foo { ... }
type Baz struct { ... }
func (b *Baz) GetBar() *Bar { ... }

But `lib1` and `lib2` both define different additional methods on some of
these structs, so I can't just merge them into a single library.

I would like to write a helper function `HelperFn(baz ??)` such that I can
call `HelperFn(&lib1.Baz{})` and also `Helperfn(&lib2.Baz{})`.

In a simple case, I can use generic interfaces:

type Foo interface {}
type Bar[FT Foo] interface {
  GetFoo() FT
}
type Baz[FT Foo, BT Bar[FT]] interface {
  GetBar() BT
}

func HelperFn[FT Foo, BarT Bar[FT], BazT Baz[FT, BarT]](baz BazT) {
  ...
}

However, AFAIK there is no way to get the compiler to infer these generics,
so to call this I have to write e.g. `HelperFn[*lib1.Foo,
*lib1.Bar](&lib1.Baz{})`.

The above is only a little inconvenient, but in my actual use case the two
libraries are generated code containing over a thousand types in an
enormous hierarchical tree, which shifts this from "a little inconvenient"
to "totally unusable" - some helpers would require >1K type parameters.

What's the recommended way to do this in Go?

Thanks,
Dan Lepage

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAAViQtiWGJc5CWD0gt8K3sXQgMSJSCNME_PqW4ESbodOdr%2BS3A%40mail.gmail.com.

Reply via email to