Value to PtrValue

5 views
Skip to first unread message

Michael Hoisie

unread,
Dec 9, 2009, 11:35:10 PM12/9/09
to golang-nuts
There's a function reflect.Indirect, which takes a Value and, if it's
a PtrValue, returns the underlying Value.

Is there an opposite of this function? Something like
reflect.AddressOf, which takes a Value and returns a PtrValue to the
underlying object? I can't seem to find it, but it would be really
useful.

Case in point: I'm trying to make a small change to the template
package, and in that code the data source is passed around by its
Value object. Once in a while I'd like to check whether or not the
Value has any methods, but you can only do this with a PtrValue.

Russ Cox

unread,
Dec 10, 2009, 1:07:53 AM12/10/09
to Michael Hoisie, golang-nuts
On Wed, Dec 9, 2009 at 20:35, Michael Hoisie <hoi...@gmail.com> wrote:
> There's a function reflect.Indirect, which takes a Value and, if it's
> a PtrValue, returns the underlying Value.
>
> Is there an opposite of this function? Something like
> reflect.AddressOf, which takes a Value and returns a PtrValue to the
> underlying object? I can't seem to find it, but it would be really
> useful.

It would be, but no. The fundamental problem is that
once you've got a value, you can't just take its address:
an expression like &3 doesn't make any sense.
If the pointer is important, the solution is to keep
it around instead of throwing it away and trying to
get it back.

Russ
Reply all
Reply to author
Forward
0 new messages