ARTICLE AD BOX
Consider the following (playground):
import "fmt" type Gizmo string func (g Gizmo) String() string { return string(g) } func Convert(in []Gizmo) []fmt.Stringer { out := make([]fmt.Stringer, 0, len(in)) for _, v := range in { out = append(out, v) } return out }Is there a way to turn Convert into a generic function like this (playground):
type AssignableTo[T any] interface { // magic goes here } func Convert[Out any, In AssignableTo[Out]](in []In) []Out { out := make([]Out, 0, len(in)) for _, v := range in { out = append(out, v) } return out }I would like to be able to use the generic function for any assignable type, not just implemented interfaces. For example:
bidiChs := []chan Foo{/* ... */} sendChs := Convert[chan<- Foo](bidiChs)(This is a re-ask because I chose the wrong question type with the new opinion-based question feature and don't see a way to fix it.)
