On Monday, March 21, 2022 at 3:29:44 PM UTC+8 Henry wrote:
> Perhaps something like this? > ```go > func Convert[T, A any](src []T, converter func(T) A) []A { > result := make([]A, len(src)) > for index, a := range src { > result[index] = converter(a) > } > return result > } > ``` > See https://play.golang.com/p/rq89Wposc-D > It might reduce some code (surely not eliminate), but it is very ungraceful. The current constraint design lacks of two abilities: 1. The ability of specifying a type argument must be an interface type. 2. The ability of specifying a type argument must not be an interface type. I except an implementation like func ConvertSlice[From implements(To), To interface](vs []From) []To { var r = make([]To, len(vs)) for i := range vs { r[i] = vs[i] } return r } > > > On Sunday, March 20, 2022 at 11:36:27 PM UTC+7 tapi...@gmail.com wrote: > >> >> >> package main >> >> type A interface { >> Ma() >> } >> >> type B interface { >> Mb() >> } >> >> type Ta struct { A } >> >> type Tb struct { B } >> >> func ConvertSliceToA[From A](vs []From) []A { >> var r = make([]A, len(vs)) >> for i := range vs { >> r[i] = vs[i] >> } >> return r >> } >> >> func ConvertSliceToB[From B](vs []From) []B { >> var r = make([]B, len(vs)) >> for i := range vs { >> r[i] = vs[i] >> } >> return r >> } >> >> func main() { >> var x [100]Ta >> var _ = ConvertSliceToA(x[:]) >> >> var y [100]Tb >> var _ = ConvertSliceToB(y[:]) >> } >> > -- 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/10d8fe73-3249-48f7-9eb1-e303a4fec69dn%40googlegroups.com.