Block evaluation from a collection of blocks

Ned Konz ned at squeakland.org
Mon Mar 22 06:23:40 UTC 2004


On Sunday 21 March 2004 9:30 pm, Jules Dubois wrote:
> Setup: I'm trying to place a collection of blocks into
>        a class variable for repeated evaluation in an
>        instance method.
>
> In a class (FWGC) method, I create a collection of blocks which call the
> class constructor, with a reference to the the collection in the class
> variable 'Moves'.
>
>     Moves == nil ifTrue:
>         [Moves := OrderedCollection
>             with: [:fwgc | FWGC f: fwgc f change
>                                 w: fwgc w
>                                 g: fwgc g
>                                 c: fwgc c]
>             with: [:fwgc |
>                  fwgc f = fwgc w
>                       ifTrue: [FWGC f: fwgc f change
>                                     w: fwgc w change
>                                     g: fwgc g
>                                     c: fwgc c]]
>
> and so on.  When I try to evaluate these blocks in an FWGC instance method
> using
>
>    Moves select: [:move  | (move value: self) validate]
>
> I get an error saying "This block requires 0 arguments".  Don't these
> blocks take 1 argument?

Sure looks like they do.

> As an alternative, I tried removing the ':fwgc' argument, replacing its
> usage as receiver with 'self', and changing the invocation to
>
>    Moves select: [:move | move value validate]
>
> In this case, it appears the 'f:w:g:c:' message is being sent to the FWGC
> class and not the fwgc instance in which the statement is bing executed.

Well, 'self' in that case would be referring to thc class, so that's not what 
you want to do.

> Am I on the wrong track or is what I'm trying to do simply impossible?

Neither. It works for me.

Try the attached. The following works fine:

	FWGC clearMoves.
	FWGC new tryMe.

What version of Squeak are you using?

Does mine work for you in a clean image?

-- 
Ned Konz
http://bike-nomad.com/squeak/
-------------- next part --------------
'From Squeak3.7alpha of 11 September 2003 [latest update: #5707] on 21 March 2004 at 10:18:40 pm'!
Object subclass: #FWGC
	instanceVariableNames: ''
	classVariableNames: 'Moves'
	poolDictionaries: ''
	category: 'Ned-test'!

!FWGC commentStamp: 'nk 3/21/2004 22:18' prior: 0!
FWGC clearMoves.
FWGC new tryMe.!


!FWGC methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:14'!
c
	^self! !

!FWGC methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:14'!
change
	^self! !

!FWGC methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:14'!
f
	^self! !

!FWGC methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:17'!
f: aFWGC w: aFWGC2 g: aFWGC3 c: aFWGC4 
	^self! !

!FWGC methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:16'!
g
	^self! !

!FWGC methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:13'!
tryMe
	self class moves do: [ :m | (m value: self) validate ]! !

!FWGC methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:17'!
validate
	^self! !

!FWGC methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:14'!
w
	^self! !


!FWGC class methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:15'!
clearMoves
	Moves _ nil! !

!FWGC class methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:08'!
f: f w: w g: g c: c
	^(self new) f: f w: w g: g c: c; yourself.! !

!FWGC class methodsFor: 'as yet unclassified' stamp: 'nk 3/21/2004 22:12'!
moves
	^ Moves
		ifNil: [Moves := OrderedCollection
						with: [:fwgc | FWGC
								f: fwgc f change
								w: fwgc w
								g: fwgc g
								c: fwgc c]
						with: [:fwgc | fwgc f = fwgc w
								ifTrue: [FWGC
										f: fwgc f change
										w: fwgc w change
										g: fwgc g
										c: fwgc c]]]! !



More information about the Squeak-dev mailing list