Re: [go-nuts] cannot register generic function with template.Funcs

2023-03-06 Thread 'Axel Wagner' via golang-nuts
No, Go generics do not exist at runtime, so there is no way for this to possibly work in generality. The best you can do is make a function that takes an `any` or a `reflect.Value` and use `reflect` to type-check at runtime, returning an error if type-checking fails: https://go.dev/play/p/c0jgBMxGd

Re: [go-nuts] cannot register generic function with template.Funcs

2023-03-06 Thread Rory Campbell-Lange
On 06/03/23, Bruno Albuquerque (b...@gmail.com) wrote: > You can easily get it to work, but not in a useful way I guess. > > https://go.dev/play/p/mq0O8OhJ1-a > > You could have a different template for each type you use I guess. Thanks for that solution. I guess I could do t.Funcs(template

Re: [go-nuts] cannot register generic function with template.Funcs

2023-03-06 Thread Bruno Albuquerque
You can easily get it to work, but not in a useful way I guess. https://go.dev/play/p/mq0O8OhJ1-a You could have a different template for each type you use I guess. -Bruno On Mon, Mar 6, 2023 at 4:43 PM Rory Campbell-Lange wrote: > Hi. I have a generic function > > func slicerG[A any](s []A,

[go-nuts] cannot register generic function with template.Funcs

2023-03-06 Thread Rory Campbell-Lange
Hi. I have a generic function func slicerG[A any](s []A, i int) []A { fmt.Println(len(s), i) if i > len(s) { return s } return s[0:i] } When I try to register this in a template's FuncMap, it fails with "cannot use generic function slicerG with