Re: [go-nuts] Is it possible to "extract" the generic [T] from a reflected type ?

2024-04-17 Thread 'Kevin Chowski' via golang-nuts
If I understand correctly, the restriction is that the compiler has to know 
all possible instantiations of a generic func/type/etc at compile time. So 
listing out each specific Hello instantiation is the workaround, which 
allows you to approximate this if you have a closed set of types you want 
to use. Assuming your Hello function truly doesn't need inputs or outputs 
it's actually pretty simple, for a closed set of types: 
https://go.dev/play/p/JAw4tXOLwYT. If you need inputs/outputs of type T 
then you have to do more hoop jumping, or just invoke the function through 
reflect.

Depending on what the rest of the code looks like, you could convince the 
compiler to do what you want if you are just passing functions statically 
and making Do a generic function too. Again, it's all about just making 
sure the compiler can transitively find all of the real instantiated types 
when things are eventually used. If you share more code maybe my example 
can be better, but here's the sort of thing I 
mean: https://go.dev/play/p/AanjJKg8Hf_i

But obviously this workaround won't work if you really need things to be 
dynamic.
On Tuesday, April 9, 2024 at 10:51:06 AM UTC-6 Ian Lance Taylor wrote:

> On Tue, Apr 9, 2024 at 9:41 AM Mihai Barbu  wrote:
> >
> > I need to call some generic functions with types that are now known at 
> compile time. Is it possible to do this?
> > See the code below (vastly reduced).
> >
> > // fv is a function that returns an unknown type
> > func Do(fv reflect.Value){
> > // get the first returned type by function fv
> > vt := fv.Type().Out(0)
> > // how to call `Hello` with `v` ?
> > // Obviously the code below is incorrect
> > Hello[vt]()
> > }
> >
> > func Hello[T any](){
> > // do something with T
> > json.Marshal(new(T))
> > }
>
> This is not possible. Sorry.
>
> Ian
>

-- 
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/4a3051a0-cba3-4f91-92a1-4954b315ff75n%40googlegroups.com.


Re: [go-nuts] Is it possible to "extract" the generic [T] from a reflected type ?

2024-04-09 Thread Ian Lance Taylor
On Tue, Apr 9, 2024 at 9:41 AM Mihai Barbu  wrote:
>
> I need to call some generic functions with types that are now known at 
> compile time. Is it possible to do this?
>  See the code below (vastly reduced).
>
> // fv is a function that returns an unknown type
> func Do(fv reflect.Value){
>  // get the first returned type by function fv
>   vt := fv.Type().Out(0)
>// how to call `Hello` with `v` ?
>   // Obviously the code below is incorrect
> Hello[vt]()
> }
>
> func Hello[T any](){
>  // do something with T
>  json.Marshal(new(T))
> }

This is not possible.  Sorry.

Ian

-- 
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/CAOyqgcVTE26WbM7UsLtuyHNvOSmELMf1ZMgVt%3DycwFq9K4o3uQ%40mail.gmail.com.


[go-nuts] Is it possible to "extract" the generic [T] from a reflected type ?

2024-04-09 Thread Mihai Barbu
I need to call some generic functions with types that are now known at 
compile time. Is it possible to do this?
 See the code below (vastly reduced).

// fv is a function that returns an unknown type
func Do(fv reflect.Value){
 // get the first returned type by function fv
  vt := fv.Type().Out(0)
   // how to call `Hello` with `v` ?
  // Obviously the code below is incorrect
Hello[vt]()
}

func Hello[T any](){
 // do something with T
 json.Marshal(new(T))
}

-- 
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/229fac8f-c948-4686-9885-1c114b662764n%40googlegroups.com.