Thread overview
Property access to a struct that contains struct
Feb 26, 2017
Guenter
Feb 26, 2017
Eugene Wissner
Feb 26, 2017
Guenter
Feb 26, 2017
Mike Parker
Feb 26, 2017
Guenter
February 26, 2017
Hi,

i do not understand where I am wrong in this code. I seems there is a missing constructor, but i have no idea where.


module main;


import std.stdio;


struct A_t
{
  int fvalue;


  A_t opCall()
  {
    return this;
  };

  @property  int value () { return fvalue; }

  @property void value (int avalue) { fvalue = avalue; }
}


struct B_t
{
  A_t  fext;


  int opCall() { return ext.value; };


  @property A_t  ext() { return fext; }

  @property void ext(A_t aext) { fext = aext; }
}



void main()
{
  B_t   test;


  test.ext.value = 9;
  int t = test();

  assert ( t == 9);   //  t  is always 0 instead of 9.


  readln();
}


cu
Guenter

February 26, 2017
On Sunday, 26 February 2017 at 11:05:42 UTC, Guenter wrote:
> Hi,
>
> i do not understand where I am wrong in this code. I seems there is a missing constructor, but i have no idea where.
>
>
> module main;
>
>
> import std.stdio;
>
>
> struct A_t
> {
>   int fvalue;
>
>
>   A_t opCall()
>   {
>     return this;
>   };
>
>   @property  int value () { return fvalue; }
>
>   @property void value (int avalue) { fvalue = avalue; }
> }
>
>
> struct B_t
> {
>   A_t  fext;
>
>
>   int opCall() { return ext.value; };
>
>
>   @property A_t  ext() { return fext; }
>
>   @property void ext(A_t aext) { fext = aext; }
> }
>
>
>
> void main()
> {
>   B_t   test;
>
>
>   test.ext.value = 9;
>   int t = test();
>
>   assert ( t == 9);   //  t  is always 0 instead of 9.
>
>
>   readln();
> }
>
>
> cu
> Guenter

It should be:
@property ref A_t  ext() { return fext; }

you return a copy of A_t and then change this copy instead of the real B_t member.
February 26, 2017
On Sunday, 26 February 2017 at 11:05:42 UTC, Guenter wrote:
> Hi,
>
> i do not understand where I am wrong in this code. I seems there is a missing constructor, but i have no idea where.

>
>   @property A_t  ext() { return fext; }

Structs in D are value types, so you're returning a copy of fext here and that's what's getting updated with the new value. Try this:

ref A_T ext() { return fext; }
February 26, 2017
On Sunday, 26 February 2017 at 11:15:21 UTC, Eugene Wissner wrote:
> you return a copy of A_t and then change this copy instead of the real B_t member.


Thank you.
Guenter
February 26, 2017
On Sunday, 26 February 2017 at 11:18:12 UTC, Mike Parker wrote:
> On Sunday, 26 February 2017 at 11:05:42 UTC, Guenter wrote:
>> Hi,
>>
>> i do not understand where I am wrong in this code. I seems there is a missing constructor, but i have no idea where.
>
>>
>>   @property A_t  ext() { return fext; }
>
> Structs in D are value types, so you're returning a copy of fext here and that's what's getting updated with the new value. Try this:
>
> ref A_T ext() { return fext; }

Thank you.
Guenter