On Mon, Nov 7, 2022 at 9:24 AM John <johnsiil...@gmail.com> wrote:
>
> Or maybe I'm just doing something wrong.
>
> I have a struct implementing generics where methods return the next method to 
> execute.
>
> For tests, I wanted to make sure the returned method was the one expected, so 
>  I was using this:
>
> nextStageName := 
> runtime.FuncForPC(reflect.ValueOf(nextStage).Pointer()).Name()
> (where "nextStage" is the next method)
>
> Here is a simple non-generic version where I get what I expect:
> https://go.dev/play/p/kimGRS7xiP2
>
> result: main.(*AStruct).NextStage-fm):
>
> Here is probably a slightly convoluted generic version that does not:
> https://go.dev/play/p/TUHUaZywki0
>
> result: main.(*GenericStruct[...]).Stage.func1
>
> In that version, it seem to give me the name of the method that returned the 
> next method.
>
> Any idea what is (or what I'm doing) wrong?

I'm not sure that runtime.FuncForPC is going to work here.  What it is
reporting is a function literal defined within the Stage method (that
is what the "func1" means).  What is happening is that the Stage
method is returning a function literal that invokes the NextStage
method.  This is due to details of how generic functions are compiled,
and may change from release to release.  In general FuncForPC is a
fairly low level operation that has no way to avoid these sorts of
compilation details.  I don't know if there is a way to do what you
want to do.

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/CAOyqgcXtYuUw%2BsX1eZb8M_UtjcGLQUOFw7WAJsmg2zn3_XEkDw%40mail.gmail.com.

Reply via email to