Why to have properties to sort or duplicate arrays ?

Pierre Renié archlinuxien at gmail.com
Thu Feb 1 13:29:42 PST 2007


renoX Wrote:

> 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.
Do you mean that in the object's internal implementation has a cache? To me it's correct. But the behavior of an object (its interface) should not be modified by reading a field.
This code :
var1 = object.field1
var2 = object.field2
var3 = object.field3
should give the same values to var1, var2 and var3 as this code :
var2 = object.field2
var1 = object.field1
var3 = object.field3

Do I have misunderstanded something?
> 
> > 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
> 
> 
> 
> 
Bill Baxter Wrote:

> Pierre Renié wrote:
> > I don't think there is a need to change the property concept. Just deprecate the properties 'sort', 'reverse' and 'dup', and create methods instead.
> 
> 
> *All* functions and methods in D that take zero or 1 arguments act like properties.
It's a big problem. If a method clear() acts like a property, does it mean that "value = object.clear" will clear my object?



More information about the Digitalmars-d mailing list