Ok, but this means I have to worry of the receiver's class
before I send collect:, so as I don't fuck it all up.... and I don't
want to think about that!!! See, if I want the collection of grades of
the students, I would like to do this:
grades
^self students collect: [.aStudent | aStudent grades]
regardless of whatever is the class of students... This does
the trick for all the collections, except for Set. And I don´t know if
students is an instance of Set, and I really don't want to think if it
is or not. If I don't want repeated students and I create my collection
as an instance of Set, an i.e.; I want to calculate the average, then
I'm screwed because the collection I get from the collect: wasn't the
want that I wanted.
So, I have to know previously that my collection is an instance
of Set, or if it isn't, so as to know if I have to convert it first....
And if I use collect: as:, as you suggest, this means the
collections are no longer polymorphic for me :(
Sorry, maybe I'm really stupid, but I don't know how that
message solves best the problem than redefining collect: for Set. For
the particular case I don't want the repeated objects (which AFAIK is
not so common) I could send asSet.
On 27 October 2011 11:05, Henrik Sperre
Johansen
<henrik.s.johansen@veloxit.no>
wrote:
On 27.10.2011 15:40, Clara Allende wrote:
I know, but our students don't :) So they ran into problems because the
message send didn't answer what they were specting.... because it
makes sense that if I want to transform the objects in my collection, I
might get repeated objects... Maybe I'm not thinking in terms of
consistency, I'm just putting myself on student's shoes :P
The consistency rule to remember for #collect:/#select:/#reject: is
that they will return a collection of the receiver's species.
#collect:as: was added as a general answer to the situation your
students ran into :)
set := Set withAll: #(1 2 3 4 5 6 7 8).
result := set collect: #even as: Bag.
Cheers,
Henry
Applying the Principle Of Least Surprise (you may notice, a favourite
of mine) to newcomers would support what you're saying. However the
flip side of POLS is the behavior expected by old hands, existing code
and code from other Smalltalk systems. I imagine there are subtle
dependencies through the system for the convention from the Blue Book
[1] which says:^(students inject: 0 into: [ :subTotal :eachStudent | subTotal +
(eachStudent grade) ] ) ) / (size students)