[Newbies] bags use

Edward Stow ed.stow at gmail.com
Wed Jun 21 03:58:46 UTC 2006


aBag := Bag new.
aBag add: ... add some of your objects.
aBag anySatisfy:  [:each | (each name = 'aName') & (each status = 'good')]

assuming that your class has implemented instance methods #name, and #status.

#anySatisfy: will work with all collection types. You not need to use a Bag.

Also do not confuse Smalltalk terminology regarding identity and equality.
Identity comparisons #== always checks to see if the lhs and rhs
objects are the same object.

objectA == objectB  -- is true if and only if objectA refers to the
same object as objectB.

Equality comparisons #=

objectA = objectB --tests to see if the two objects are equivalent.

If you want to perform equality tests between objects then define the
#= instance method for the objects class.  You should also define
#hash.

See class Point for examples for implementing #= and #hash.


On 21/06/06, Benjamin SALLAS <benjamin.sallas at wanadoo.fr> wrote:
> hello,
> I'm trying to use bags correctly, and i need a little help.
> I'd like to create a bag with some instance of a class with variables (for example: name, status).
> I'd like to make the identity of the bag on the name only, and be able in a loop to get the status of each item.
> With my example, the status is 'good' or 'bad', i want to check if one of the item with a certain name has the status 'good'.
> Could someone explain me how i can do that?
>
> benjamin
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


-- 
---
Edward Stow


More information about the Beginners mailing list