Design question regarding saving changes in the original array and/or returning a new set

matheus matheus at gmail.com
Sun Oct 23 13:32:44 UTC 2022


Hi,

I have a design question and I'd like to hear some advice. Let's 
say that I want to create a method to sort an array:

     arr.sort(asc);

I think usually this would usually return a new set of that array 
but now sorted.

But If I want to do this in the original, I think I would do this:

     arr.sort(asc).save();

The problem with this, it would create a new set and assign/copy 
back to the caller, which would be a waste. So I thought about 
this:

     arr.save.sort(asc);

Then save would tell to "sort()" what to do beforehand, like 
passing some argument saying that the sort should be done direct 
in the caller and no copy.

Is this (The latter) an approach you would use? Or there is a 
better way to do this.

By the way in this design I'd like to have both options, return a 
new set and/or change the original.

Thanks in advance,

Matheus.


More information about the Digitalmars-d-learn mailing list