Dynamic conversion between generic interfaces of different types

73 Views Asked by At

Suppose I have following generic interface:

type Iface[T any] interface {
    MyFunc(T)
}

I would like to implement "adapter" or "converter" class, which helps to convert between interfaces of different types. Essentially, it wraps Iface[A] to behave like an Iface[B]:

type Adapter[A any, B any] interface {
    Adapt(Iface[A]) Iface[B]
}

So, I can't figure out how to write Adapt function:

type Iface[T any] interface {
    MyFunc(T)
}

type Adapter[A any, B any] interface {
    Adapt(Iface[A]) Iface[B]
}

type StringToIntAdapter struct {
}

func (StringToIntAdapter) Adapt(Iface[string]) Iface[Int] {
    // ???
}

I think I would need to somehow dynamically create a new instance of an interface inside of an adapter. But I can't find any tips out how to do so.

0

There are 0 best solutions below