Accessing Private Fields from Outside

Mehrdad wfunction at hotmail.com
Tue May 17 17:56:58 PDT 2011


On 5/17/2011 5:52 PM, Robert Jacques wrote:
> On Tue, 17 May 2011 20:44:24 -0400, Mehrdad <wfunction at hotmail.com> 
> wrote:
>
>> On 5/17/2011 5:13 PM, Robert Jacques wrote:
>> T.tupleof has a problem though: It doesn't seem to let me actually 
>> access the value; it just give me a tuple I can't do anything with. 
>> Your example only gets the name of the field, but you never actually 
>> seem to access it.
>>
>> (P.S.: I'm trying to marshal from/to Windows data structures, so I 
>> guess it's kinda both?)
>
> I actually do both (name and value) in the above example.
> Name: value.tupleof[i].stringof["value.".length..$]
> Value: v
> You can also use value.tupleof[i] if all you want is to set a field.

Hm, my bad. Seems like the newest version of DMD allows this, though I 
could swear I couldn't do that before (it would say the member is 
inaccessible). :-)
(And yeah, I already noticed that thing about foreach(ref), thanks.)

On 5/17/2011 5:56 PM, Adam D. Ruppe wrote:
>> Thanks, but traits doesn't really let me read or write to the
>> variable though. :(
> Use getMember there.
>
> foreach(member; __traits(allMembers, TYPE))
>     __traits(getMember, instance_of_type, member) = something;
>
> tupleof is probably better for this though, since it only includes
> actual data members; it excludes methods.
>
> You can write to tupleof by using an index.

Nope, getMember doesn't work, it gives me an inaccessible field error.


More information about the Digitalmars-d mailing list