From: MonkeeSage Date: 2007-12-30T10:19:58+09:00 Subject: Re: Add Array#first= and Array#last= to std lib On Dec 29, 6:06 pm, Gary Wright wrote: > On Dec 29, 2007, at 6:24 PM, MonkeeSage wrote: > > > I wasn't arguing for or against #last=, per se; I was only trying to > > explain why it doesn't make sense, _to me_, "Unless the semantics [of > > #last] change," to have #last=. I.e., #[] subscripts the array and > > returns the value of the index, but #last returns the value of > > subscripting the array at index #length-1; it is another level of > > abstraction removed from #[]; so with those semantics, #last= would be > > like #length= (or #pop=), and you end up with exceptions when the > > value is immutable. > > Why do you shift your terminology when talking about last? Using > your terminology but my mental model of arrays: > > #[n] subscripts the array with n and returns the value of the index > #[-1] subscripts the array with (length-1) and returns the value of > the index > #last subscripts the array with (length-1) and returns the value of > the index > > Why do you rephrase that last case as: > > #last returns the value of subscripting the array at index #length-1 > > Are you suggesting that #last does something distinctly different than > subscripting via -1? If not then it would seem your argument against > last= > would apply just as well to 'array[-1] = x'. Are you suggesting that > element assignment with negative subscripts also don't make sense to > you? > > Objections to last= and first= based on redundancy is one thing, but > you seem to be objecting based on semantics and I'm trying to understand > that objection because the semantics seem to be quite natural to me. > > Gary Wright As I understand it, #last is semantically equivalent to this code: class Array def last self[self.length-1] end end Which means that #last is a specialized form of #[], which means that it is another level of abstraction removed from #[]. So the result of #last is the same as #[-1], but the semantics of #last are more like #length or #pop, which are also a level of abstraction removed from #[]. Once again, I'm not arguing against #last=, per se, but I fail to see how it differs at the present time from, for example: class Array def foo self[self.length-1] end end [1,2,3].foo = 25 Or more simply, [1,2,3].length = 20. Of course, one could implement those methods where they so inclined, but it would make the semantics of, e.g., #foo and #foo= very different. In this case of #last / #first, I don't think it is very confusing to have those different semantics; I was probably being too much of a stickler for "purism". Regards, Jordan