Passing dynamic arrays

Jens Mueller jens.k.mueller at gmx.de
Tue Nov 9 02:43:57 PST 2010


> > What's the general use of a = new A() in the above code? Where is it
> > useful?
> > 
> > Jens
> 
> I don't really have any good use-case examples. Maybe an initialization function? Developed your own number object (big int) and were thinking in terms of it being a refrence you thought 
> 
> a = a + BigInt(7);
> 
> would result in a being resigned in the calling function. Or maybe just a function that swaps two class references:
> 
> void swap(T)(T a, T b) { // Correct void swap(T)(ref T a, ref T b) {
>     auto tmp = a; a = b; b = tmp;
> }
> 
> Actually that turned out to be a pretty good one.

I do see why a = new A() is useful. But it makes only sense if I passed
a/b as ref a/b. Basically I wonder why I do not get a warning when
changing the reference in that situation. So my question is more why am
I allowed to change the reference even though I didn't pass it as ref a.
I was looking for a use of that. Assuming there is good use then there
is no reason to forbid it. But if there is no good use I'd like to be
warned when compiling the above swap. Because it's an error.
For dynamic arrays slicing is a good example to allow it. But why should
it be allowed for objects?

Jens


More information about the Digitalmars-d mailing list