Make Map a function not a method (not sure it can be made a method they way you’re trying):
package main import ( "fmt" "strconv" ) type List[T any] []T func ToList[T any](v []T) List[T] { return List[T](v) } func Map[T, U any](l List[T], f func(v T) U) List[U] { r := make(List[U], len(l)) for i, x := range l { r[i] = f(x) } return r } func main() { x := []int{1, 2, 3} y := ToList(x) fmt.Println(y) fmt.Printf("%q\n", Map(y, strconv.Itoa)) } https://go2goplay.golang.org/p/F8ZojZF-ZPL <https://go2goplay.golang.org/p/aOzuBVwfedj> > On Feb 22, 2021, at 7:03 AM, Khosrow Afroozeh <kh.afroo...@gmail.com> wrote: > > I was playing with the go2go playground, and while implementing a toy List > type, came across a few issues (Sorry if these have been discussed before, my > cursory search didn't turn out to find anything). Given the type > > type List[T any] []T > > 1. The current go2go implementation does not allow one to do this: > > func ToList[T any](v []T) List[T] { > return List(v) > } > > with the error: List(v) is not a type > > Is this a bug, shortcoming of the current implementation, or by design? This > would be a deal breaker if type-casting doesn't work for generics. > > 2. It seems impossible to implement a type-safe Map function. The following > code: > > func (l List[T]) Map(f func[U any](v T) U) List[U] { > return nil > } > > will not compile, with the error: function type cannot have type parameters > Judging by the error message this seems to be by design, but it will > significantly reduce the usability of generics for any transformation method. > What is the reasoning behind this limitation? > > Thanks, > > -- > 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 > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/2aa1c2cb-e7bc-438e-81a9-e4a2904af21cn%40googlegroups.com > > <https://groups.google.com/d/msgid/golang-nuts/2aa1c2cb-e7bc-438e-81a9-e4a2904af21cn%40googlegroups.com?utm_medium=email&utm_source=footer>. -- 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/44EBFBBB-71E4-43C4-82E7-3191D5D50DBE%40k0dvb.com.