Reading about D: few questions

Mr. Anonymous mailnew4ster at gmail.com
Thu Dec 29 04:08:50 PST 2011


On 25.12.2011 11:28, Denis Shelomovskij wrote:
> OK. As I wrote: "Yes, this allocation sometimes can be optimized out but
> not always.". Consider this:
> ---
> void main()
> {
> int[] a = new int[5];
> void f(int[] b)
> {
> // Here we assume that b is unchanged a.
> // As these array differ we need a copy.
> assert(b[0] == 0);
> assert(a[0] == 1);
> }
> f(a[]++); // Note: compilation error now
> }
> ---
> Why not to rewrite `f(a[]++);` as `f(a); ++a[];`? Because postincrement
> is expected to increment its argument when it is executed. It just
> returns an unchanged copy. Analogous D code with integers illustrates this:
> ---
> void main()
> {
> int a;
> void f(int b)
> {
> assert(b == 0);
> assert(a == 1);
> }
> f(a++);
> }
> ---
I wasn't familiar with this postincrement behavior. I would expect both 
a and b to be 0 inside f().
Anyway, what do you suggest? To leave it not compilable?

My original point was to just allow:
a[]++;
More complicated scenarios like the one above is beyond my knowledge, as 
I'm only learning the language, but I think they have to be addressed as 
well.


More information about the Digitalmars-d-learn mailing list