Reference to an object disregarding mutation - what's the trick?
Daniel Keep
daniel.keep.lists at gmail.com
Mon May 28 19:10:26 PDT 2007
Your other problem's been addressed, but I just wanted to comment on
Steve Teale wrote:
> OK, so I'm in a typical tree situation:
>
> int n = node.parent.nodelist.length;
> node.parent.nodelist.length = n+1;
> // p.s. why can't I say node.parent.nodelist.length++ - doesn't work
This is probably one of big warts with properties in D. An array's
".length" isn't just a data field. Because assigning to it can cause
other things to happen (like a reallocation), it's a property. And
because of the way properties are implemented in D, they cannot act as
lvalues.
Short version: You can't ++, --, op= a property, or pass it by reference.
If you're still wondering what the difference is:
struct LengthIsAField
{
size_t length;
}
struct LengthIsAProperty
{
size_t length() { ... }
size_t length(size_t value) { ... }
}
Both are accessed using "instance.length", but the latter can't be used
as an lvalue.
Hope that helps :)
-- Daniel
--
int getRandomNumber()
{
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
http://xkcd.com/
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d
mailing list