Array efficiency & rendering buffers

Deewiant deewiant.doesnotlike.spam at gmail.com
Thu Jun 8 02:02:05 PDT 2006


Henrik Eneroth wrote:
> On a side note, I realised that using the += operator with a dynamic array
> length property didn't work. I guess the (a = a + 1) <=> (a += 1) rationale
> doesn't work on arrays (since b.length = b.length + 1 does indeed work, whereas
> b.length += 1 does not). Wouldn't that be neat though?
> 

It's not about arrays, it's about properties in general.

See, in D, a function call f(x) can also be written f = x. So "array.length =
array.length + 1" is actually equivalent to "array.length(array.length + 1)":
length is just a function. "array.length += 1" is like writing "array.length()
+= 1" which doesn't make sense to the compiler, since you can't add a value to a
function.

This syntax makes it easier to write and use properties without needing special
get/set syntax for them, but it does have the drawback of not being able to use
the rest of the assign operators.

Plus, it allows you to write some pretty strange-looking stuff, like:

writefln = "foo bar";
writefln = (toString = 42);

Fortunately, unless obfuscating on purpose, people realise to avoid writing such
code. ;-)



More information about the Digitalmars-d-learn mailing list