Hi @globalflea,
It is indeed possible that an interface method’s argument refers to the interface type - see this article for details:
How to use generics for creating self-referring interfaces
TL;DR:
Step 1. The Cloner interface stays unchanged.
type Cloner[C any] interface {
Clone() C
}
C is not restricted to anything yet, but step 2 takes care of this.
Step 2. A function that clones any type uses Cloner[T]
as type restriction:
func CloneAny[T Cloner[T]](c T) T {
return c.Clone()
}
Then, for any variable s
whose type implements Cloner
, you can simply call
CloneAny(s)
to clone s.
The blog article explains this solution in more detail.