Note for posterity:

Other methods of method equality fall in the same problem, such as detailed 
in:
https://stackoverflow.com/questions/9643205/how-do-i-compare-two-functions-for-pointer-equality-in-the-latest-go-weekly

Proof: https://go.dev/play/p/aeeRKfderY2

On Monday, November 7, 2022 at 10:09:26 AM UTC-8 John wrote:

> Thanks Ian for being generous with your time in answering this.  I'll see 
> if I can drum up another way to make that check work. 
>
> Cheers and have a good rest of your day.
> On Monday, November 7, 2022 at 10:02:28 AM UTC-8 Ian Lance Taylor wrote:
>
>> On Mon, Nov 7, 2022 at 9:24 AM John <johns...@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/bf7f9802-0dd6-4ef0-ad7c-fbc6ba00feb4n%40googlegroups.com.

Reply via email to