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

H. S. Teoh hsteoh at qfbox.info
Sun Oct 23 14:11:47 UTC 2022


On Sun, Oct 23, 2022 at 01:32:44PM +0000, matheus via Digitalmars-d-learn wrote:
> 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.

No, it does not.  It sorts `arr` in-place.


> But If I want to do this in the original, I think I would do this:
> 
>     arr.sort(asc).save();

That's not necessary, the .save is completely redundant.


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

If you want a new sorted array without touching the original, do this
instead:

	auto sortedArr = arr.dup.sort();


T

-- 
They say that "guns don't kill people, people kill people." Well I think the gun helps. If you just stood there and yelled BANG, I don't think you'd kill too many people. -- Eddie Izzard, Dressed to Kill


More information about the Digitalmars-d-learn mailing list