Passing dynamic arrays

Jens Mueller jens.k.mueller at gmx.de
Wed Nov 10 00:26:13 PST 2010


> > I see your point. You argue that the behavior is consistent. My point is
> > that this consistency can lead to bugs. I may forget the ref. But I'll
> > keep in mind to never forget the ref if it is needed.
> > 
> > Jens
> 
> Well, in the case of classes, I don't think it would be very common.
> 
> For arrays it can be nice since you can assign back a slice of the array that you want to work with.
> 
> void main(string args) {
>     args = args[1..$];
> }
> 
> Otherwise I suggest you start labeling all parameters with in. Then you are prevented from modifying the reference, and can decide if it should be a ref parameter. Who knows, maybe you'll find a reason to leave it off.

With dynamic arrays I totally agree. Slicing is very useful. I just want
a safe rule to work with it. Maybe that's not needed anymore because by
now I spend enough time on this that probably I'll never forget.
With in I also disallow changing the object itself, right? I want to
only forbid the changing of the reference of an argument inside the
function. With in/const I disallow every change. I may want to change
data of an array.

Jens

PS
The more we talk about it, the more I come to the conclusion that this
is just something you need to know. I can live with that.


More information about the Digitalmars-d mailing list