Why to have properties to sort or duplicate arrays ?

Derek Parnell derek at psych.ward
Sat Jan 27 18:55:02 PST 2007


On Sat, 27 Jan 2007 18:00:10 -0500, Pierre Renié wrote:

> Hello,
> To me, reading a field or a property should not modify the object.
> The problem is that just an access to the properties 'reverse' 
> or 'sort' are modifying my array. These 2 properties should be
> method instead.
> 
> I think that the property 'dup' should be a method too, because
> 'dup' is not a value of my Array object, it does something.

Yes, this is a significant design mistake that has always been with us.

I suspect it came about with the desire to make properties easy to create
and use, but as a consequence, D properties aren't as useful as they could
be. They are certainly a good thing but still not as good as they could
have been.

I believe that a 'get' Property should return a value without changing the
entity that owns the value. 

I don't have an issue with the .dup property in this regard as it returns a
copy of the entity; however it is not built-in to all datatypes, just
arrays.

But .sort and .reverse should just return a copy of the data, sorted or
reversed respectively.

I'm pretty sure that Walter will not be changing this or improving the
Property concept any time soon though. So use this idiom instead ...

   (sorted_data = data.dup).sort;
   (reversed_data = data.dup).reverse;

-- 
Derek Parnell



More information about the Digitalmars-d mailing list