On 01.12.2014, at 07:35, Colin Putney <[hidden email]> wrote:
That’s certainly simple: “collect: returns a collection of the same class as the receiver, except for weak collections, which answer non-weak equivalents. To return a collection of a different class, use collect:as:" - Bert - smime.p7s (5K) Download Attachment |
In reply to this post by Ben Coman
On Fri, Nov 28, 2014 at 7:42 PM, Ben Coman <[hidden email]> wrote: Levente Uzonyi wrote: No. That's what species was invented for in the first place. Which defaults to "^self species" and is overidden as required. best,
Eliot |
In reply to this post by Bert Freudenberg
On Mon, Dec 1, 2014 at 2:35 AM, Bert Freudenberg <[hidden email]> wrote:
except collect: can't possibly do this for Interval. IMO, reify the idea of species in the comments for collect: and species and say that collect: answers something of the same species. The idea being that species is the same as class when the receiver is a mutable collection, but some other suitable class when it doesn't. best,
Eliot |
On Mon, Dec 1, 2014 at 10:29 AM, Eliot Miranda <[hidden email]> wrote:
> > > On Mon, Dec 1, 2014 at 2:35 AM, Bert Freudenberg <[hidden email]> > wrote: >> >> On 01.12.2014, at 07:35, Colin Putney <[hidden email]> wrote: >> >> >> On Sun, Nov 30, 2014 at 6:20 AM, Bert Freudenberg <[hidden email]> >> wrote: >> >>> >>> How would you define “type” here? Same class? >> >> >> Yes, same class. It might be reasonable to answer instances of analogous >> classes for weak collections, and expect the caller to use #collect:as: if >> they want to retain weakness. >> >> Colin >> >> >> That’s certainly simple: >> >> “collect: returns a collection of the same class as the receiver, except >> for weak collections, which answer non-weak equivalents. To return a >> collection of a different class, use collect:as:" > > > except collect: can't possibly do this for Interval. IMO, reify the idea of Not only that, but the "except for weak collections" is a totally arbitrary exception, because the collected objects may or may not be referenced from elsewhere. > species in the comments for collect: and species and say that collect: > answers something of the same species. The idea being that species is the > same as class when the receiver is a mutable collection, but some other > suitable class when it doesn't. |
In reply to this post by Bert Freudenberg
On Tue, Dec 2, 2014 at 1:20 AM, stepharo <[hidden email]> wrote:
These are from Smalltalk-80 v2 of Object methods for private species "Answer the preferred class for reconstructing the receiver. For example, collections create new collections whenever enumeration messages such as collect: or select: are invoked. The new kind of collection is determined by the species of the original collection. Species and class are not always the same. For example, the species of Interval is Array." ^self class Collection methods for enumerating collect: aBlock "Evaluate aBlock with each of the receiver's elements as the argument. Collect the resulting values into a collection that is like the receiver. Answer the new collection. " | newCollection | newCollection _ self species new. self do: [:each | newCollection add: (aBlock value: each)]. ^newCollection select: aBlock "Evaluate aBlock with each of the receiver's elements as the argument. Collect into a new collection like the receiver, only those elements for which aBlock evaluates to true. Answer the new collection." | newCollection | newCollection _ self species new. self do: [:each | (aBlock value: each) ifTrue: [newCollection add: each]]. ^newCollection Interval methods for private species ^Array But there are also already uses for comparing: CharacterBlock methods for comparing = aCharacterBlock self species = aCharacterBlock species ifTrue: [^stringIndex = aCharacterBlock stringIndex] ifFalse: [^false] Interval methods for comparing = anInterval "Answer true if my species and anInterval species are equal, and if our starts, steps and sizes are equal." self species == anInterval species ifTrue: [^start = anInterval first and: [step = anInterval increment and: [self size = anInterval size]]] ifFalse: [^false] However, the species comment clearly states the usage pattern, even though it fails to mention the intent (to provide a mutable container with which to derive a new collection from the original).
+1
best,
Eliot |
In reply to this post by Bert Freudenberg
On Tue, Dec 2, 2014 at 1:20 AM, stepharo <[hidden email]> wrote:
These are from Smalltalk-80 v2 of April 1, 1983 on 31 May 1983 at 9:10:35 am Object methods for private species "Answer the preferred class for reconstructing the receiver. For example, collections create new collections whenever enumeration messages such as collect: or select: are invoked. The new kind of collection is determined by the species of the original collection. Species and class are not always the same. For example, the species of Interval is Array." ^self class Collection methods for enumerating collect: aBlock "Evaluate aBlock with each of the receiver's elements as the argument. Collect the resulting values into a collection that is like the receiver. Answer the new collection. " | newCollection | newCollection _ self species new. self do: [:each | newCollection add: (aBlock value: each)]. ^newCollection select: aBlock "Evaluate aBlock with each of the receiver's elements as the argument. Collect into a new collection like the receiver, only those elements for which aBlock evaluates to true. Answer the new collection." | newCollection | newCollection _ self species new. self do: [:each | (aBlock value: each) ifTrue: [newCollection add: each]]. ^newCollection Interval methods for private species ^Array But there are also already uses for comparing: CharacterBlock methods for comparing = aCharacterBlock self species = aCharacterBlock species ifTrue: [^stringIndex = aCharacterBlock stringIndex] ifFalse: [^false] Interval methods for comparing = anInterval "Answer true if my species and anInterval species are equal, and if our starts, steps and sizes are equal." self species == anInterval species ifTrue: [^start = anInterval first and: [step = anInterval increment and: [self size = anInterval size]]] ifFalse: [^false] However, the species comment clearly states the usage pattern, even though it fails to mention the intent (to provide a mutable container with which to derive a new collection from the original).
+ 1
best,
Eliot |
Free forum by Nabble | Edit this page |