Why to have properties to sort or duplicate arrays ?

renoX renosky at free.fr
Mon Jan 29 10:07:27 PST 2007


Pierre Renié Wrote:
> Hello,
> To me, reading a field or a property should not modify the object.

That's debatable, I remember an example of complex Class in a book which memorized the attributes when asked to convert say from x,y to length,angle.

> The problem is that just an access to the properties 'reverse' or 'sort' are modifying my array. These 2 properties should be method instead.

IMHO that's not the real problem, the real problem is looking at the function name, you have no clue whether they do it in place or functional style.
What I would prefer is that the name or reverse and sort indicate that they are modifying the array: like in Ruby we would have sort! and sort, sort! doing in place sort, and sort returning a sorted array (not modifying the input array).
 
> I think that the property 'dup' should be a method too, because 'dup' is not a value of my Array object, it does something.

On the whole I agree: functions which do something obvious shouldn't look like attributes, but it's also interesting to have attributes reading/setting look like attributes reading/writing whether they are implemented by function or not..

renoX







More information about the Digitalmars-d mailing list