Re: [go-nuts] How to get explicit receiver of a method through reflect?

2017-11-23 Thread Josh Humphries
If you knew the representation of the function, and how captured variables
were stored in this representation, you could use unsafe to reinterpret the
function value as a usable form (pointer, tuple, whatever) and de-reference
this data to read captured variable addresses. In the case you describe,
the receiver would be the only captured variable.

But this is a very bad idea for a couple of reasons:

   1. The Go language spec does not define the representation for a
   function. So if you discovered the representation, the code would not be
   portable to other Go compilers or even across versions of Go.
   2. It is not safe. You have to know apriori the number and types of
   closed variables for this to work. You would not be able to distinguish
   between functions that have the same signature, but with different sets of
   closed variables. So it could be possible you receive a function that does
   not capture a method receiver, and then your interpretation of whatever you
   read using unsafe will likely cause memory corruption and/or a program
   crash.




*Josh Humphries*
jh...@bluegosling.com

On Wed, Nov 22, 2017 at 8:46 PM, Hoping White  wrote:

> Thanks Josh. Yes, a single-method interface can do the job. I just want to
> know the unsafe way for curiosity. could you please explain more?
>
> 2017-11-22 22:12 GMT+08:00 Josh Humphries :
>
>> The reflection package provides no way to do this. Even if it were
>> possible to do with unsafe (not even sure it is, but maybe?), it would be
>> brittle and tied to an undocumented representation of a function and its
>> captured variables.
>>
>> Instead, use a single-method interface. It's easy to create a factory
>> method that takes a function and adapts it to the interface. And then the
>> value can be used both to invoke the logic (by calling the one method of
>> the interface) and for inspecting its concrete type and value.
>>
>>
>> 
>> *Josh Humphries*
>> jh...@bluegosling.com
>>
>> On Wed, Nov 22, 2017 at 8:26 AM, Hoping White 
>> wrote:
>>
>>> Hi, all
>>>
>>> I known that method of a struct can be a function with explicit
>>> receiver, like this
>>>
>>> type Param struct {
>>> v int
>>> }
>>>
>>> func (this *Param) Call() {
>>> println(this.v)
>>> }
>>>
>>> p := {v:10}
>>> t := p.Call
>>> t()
>>>
>>> I wonder how can I get the receiver p from function t through
>>> reflect. Thanks all.
>>>
>>> --
>>> 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.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How to get explicit receiver of a method through reflect?

2017-11-22 Thread Hoping White
Thanks Josh. Yes, a single-method interface can do the job. I just want to
know the unsafe way for curiosity. could you please explain more?

2017-11-22 22:12 GMT+08:00 Josh Humphries :

> The reflection package provides no way to do this. Even if it were
> possible to do with unsafe (not even sure it is, but maybe?), it would be
> brittle and tied to an undocumented representation of a function and its
> captured variables.
>
> Instead, use a single-method interface. It's easy to create a factory
> method that takes a function and adapts it to the interface. And then the
> value can be used both to invoke the logic (by calling the one method of
> the interface) and for inspecting its concrete type and value.
>
>
> 
> *Josh Humphries*
> jh...@bluegosling.com
>
> On Wed, Nov 22, 2017 at 8:26 AM, Hoping White 
> wrote:
>
>> Hi, all
>>
>> I known that method of a struct can be a function with explicit receiver,
>> like this
>>
>> type Param struct {
>> v int
>> }
>>
>> func (this *Param) Call() {
>> println(this.v)
>> }
>>
>> p := {v:10}
>> t := p.Call
>> t()
>>
>> I wonder how can I get the receiver p from function t through
>> reflect. Thanks all.
>>
>> --
>> 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.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] How to get explicit receiver of a method through reflect?

2017-11-22 Thread Josh Humphries
The reflection package provides no way to do this. Even if it were possible
to do with unsafe (not even sure it is, but maybe?), it would be brittle
and tied to an undocumented representation of a function and its captured
variables.

Instead, use a single-method interface. It's easy to create a factory
method that takes a function and adapts it to the interface. And then the
value can be used both to invoke the logic (by calling the one method of
the interface) and for inspecting its concrete type and value.



*Josh Humphries*
jh...@bluegosling.com

On Wed, Nov 22, 2017 at 8:26 AM, Hoping White  wrote:

> Hi, all
>
> I known that method of a struct can be a function with explicit receiver,
> like this
>
> type Param struct {
> v int
> }
>
> func (this *Param) Call() {
> println(this.v)
> }
>
> p := {v:10}
> t := p.Call
> t()
>
> I wonder how can I get the receiver p from function t through
> reflect. Thanks all.
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] How to get explicit receiver of a method through reflect?

2017-11-22 Thread Hoping White
Hi, all

I known that method of a struct can be a function with explicit receiver,
like this

type Param struct {
v int
}

func (this *Param) Call() {
println(this.v)
}

p := {v:10}
t := p.Call
t()

I wonder how can I get the receiver p from function t through
reflect. Thanks all.

-- 
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.
For more options, visit https://groups.google.com/d/optout.