www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Property change problem

reply Zarathustra <adam.chrapkowski gmail.com> writes:
I have got following code:

________________________________________________________
module main;
import std.stdio;
import std.process;

struct SSome{ 
  long a = 0;
  long b = 0;
}
class CFoo{
  private SSome m_u;
  private SSome m_v;

  public SSome u(SSome o_u){ return this.m_u = o_u; }
  public SSome u(         ){ return this.m_u;       }

  public SSome v(SSome o_v){ return this.m_v = o_v; }
  public SSome v(         ){ return this.m_v      ; }
}

class CBar{
  private CFoo m_foo;

  public CFoo foo(CFoo o_foo){ return this.m_foo = o_foo; }
  public CFoo foo(          ){ return this.m_foo        ; }

  this()
  body{
    this.m_foo = new CFoo;
  }
}

void
main(char [][] args){
  SSome l_some = SSome(1, 2);
  CFoo l_foo = new CFoo;
  writefln("l_foo.u.a: ", l_foo.u.a);
  l_foo.u = l_some;
  writefln("l_foo.u.a: ", l_foo.u.a);
  CBar l_bar = new CBar;
  writefln("l_bar.foo.u.a: ", l_bar.foo.u.a);
  l_bar.foo.u.a = 5;   // no effect
  writefln("l_bar.foo.u.a: ", l_bar.foo.u.a);
  system("pause");
}
________________________________________________________
result:
  l_foo.u.a: 0
  l_foo.u.a: 1
  l_bar.foo.u.a: 0
  l_bar.foo.u.a: 0
________________________________________________________
How to easy change value of property l_bar.foo.u.a? Because the above way
doesn't work.
Jul 21 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Zarathustra" <adam.chrapkowski gmail.com> wrote in message 
news:g62dv7$gao$1 digitalmars.com...

  l_bar.foo.u.a = 5;   // no effect

I really wish the compiler could catch stuff like this. It has no effect because it's doing something like this: auto temp = l_bar.foo.u; temp.a = 5; And then throwing away 'temp'. That is, it's not modifying l_bar.foo.u, it's modifying a temporary copy of it. It would work if u() returned an SSome* (that is, "return &this.m_u;"), but I don't know if that's acceptable for you.
Jul 21 2008
parent reply "Koroskin Denis" <2korden gmail.com> writes:
On Mon, 21 Jul 2008 21:26:04 +0400, Jarrett Billingsley  
<kb3ctd2 yahoo.com> wrote:

 "Zarathustra" <adam.chrapkowski gmail.com> wrote in message
 news:g62dv7$gao$1 digitalmars.com...

  l_bar.foo.u.a = 5;   // no effect

I really wish the compiler could catch stuff like this. It has no effect because it's doing something like this: auto temp = l_bar.foo.u; temp.a = 5; And then throwing away 'temp'. That is, it's not modifying l_bar.foo.u, it's modifying a temporary copy of it. It would work if u() returned an SSome* (that is, "return &this.m_u;"), but I don't know if that's acceptable for you.

That's why we need C++-style references!
Jul 21 2008
parent reply Zarathustra <adam.chrapkowski gmail.com> writes:
Koroskin Denis Wrote:

 On Mon, 21 Jul 2008 21:26:04 +0400, Jarrett Billingsley  
 <kb3ctd2 yahoo.com> wrote:
 
 "Zarathustra" <adam.chrapkowski gmail.com> wrote in message
 news:g62dv7$gao$1 digitalmars.com...

  l_bar.foo.u.a = 5;   // no effect

I really wish the compiler could catch stuff like this. It has no effect because it's doing something like this: auto temp = l_bar.foo.u; temp.a = 5; And then throwing away 'temp'. That is, it's not modifying l_bar.foo.u, it's modifying a temporary copy of it. It would work if u() returned an SSome* (that is, "return &this.m_u;"), but I don't know if that's acceptable for you.

That's why we need C++-style references!

Hmm it's realy strange and in my opinion it harms the properties idea. Maybe, one day it will be changed. I hoped that was only my mistake :(. Ok, thanks a lot for help and if anybody know other solution I would like to know it.
Jul 21 2008
parent "Manfred_Nowak" <svv1999 hotmail.com> writes:
Zarathustra wrote:

 it harms the properties idea

What is your idea of a property? -manfred -- Maybe some knowledge of some types of disagreeing and their relation can turn out to be useful: http://blog.createdebate.com/2008/04/07/writing-strong-arguments/
Jul 22 2008