www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - property: feature enhancement

reply ehance d.com writes:
From D doc:

int test()
{
Foo f;

f.data = 3;		// same as f.data(3);
return f.data + 3;	// same as return f.data() + 3;
}

So how about:

f.data += 3;   // same as f.data( f.data()+3 )
f.data -= 3;   // same as f.data( f.data()-3 )
f.data *= 3;   // same as f.data( f.data()*3 )
f.data /= 3;   // same as f.data( f.data()/3 )

=
<<= ..
May 14 2005
parent reply "Uwe Salomon" <post uwesalomon.de> writes:
 So how about:

 f.data += 3;   // same as f.data( f.data()+3 )
 f.data -= 3;   // same as f.data( f.data()-3 )
 f.data *= 3;   // same as f.data( f.data()*3 )
 f.data /= 3;   // same as f.data( f.data()/3 )
The op= operators are guaranteed to evaluate the lvalue only once. If you rewrite it the way you propose, this would not be true anymore. Ciao uwe
May 15 2005
parent Chris Sauls <ibisbasenji gmail.com> writes:
Uwe Salomon wrote:
 f.data += 3;   // same as f.data( f.data()+3 )
 f.data -= 3;   // same as f.data( f.data()-3 )
 f.data *= 3;   // same as f.data( f.data()*3 )
 f.data /= 3;   // same as f.data( f.data()/3 )
The op= operators are guaranteed to evaluate the lvalue only once. If you rewrite it the way you propose, this would not be true anymore.
As far as I can see, (T prop(T)) is still only being called once. Is there some case wherein the Settor (T prop(T)) needs to emulate a Gettor (T prop()), thereby causing an internal change before the operation? If not, then I don't see how its no longer true. -- Chris Sauls
May 19 2005