[Newbies] subclasses detect

David Mitchell david.mitchell at gmail.com
Tue Jul 31 20:50:32 UTC 2007

The message #subclasses returns a collection of Class objects
The message #detect: returns the first object in that collection that
matches the criteria in the block

Since detect is the last message sent before the return, one class
object will be returned.

If more than one matches, the code will never know, since detect:
returns on the first match. It won't evaluate to find the other match.
That is, it short circuits the iteration.

If you want to return a collection of matches, send the #select:
message instead of #detect:.

Nothing to do with class instance variables (those are rare birds --
misused as often as they are needed).

Also realize that the message isn't "subclasses detect". They are two
separate messages. #detect: works with any collection. #subclasses
returns a collection.

On 7/31/07, gruntfuttuck <gruntfuttuck at gmail.com> wrote:
> Hello
> In the laser game tutorial by Stephan B Wessels I was intrerested by this
> code:
> directionFor: aSymbol
>         ^ self subclasses
>                 detect: [:cls | cls directionSymbol = aSymbol]
> The code appears here at the bottom of the page:
> http://squeak.preeminent.org/tut2007/html/038.html
> What I read this code as doing, in this example is: return an symbol object
> which is a sublass instance varable, if it is the same as aSymbol.
> What would happen if more than one subclass object had a match?
> Also how else can subclasses detect: be used? It looks very interesting.
> Grunt
> --
> View this message in context: http://www.nabble.com/subclasses-detect-tf4196202.html#a11934239
> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

More information about the Beginners mailing list