Full Closures - when do they close?

Robert Withers withers at vnet.net
Mon Jan 31 23:06:57 UTC 2000


Vassili Bykov wrote:
> 
> From: slosher at vnet.net [mailto:slosher at vnet.net]On Behalf Of Robert
> Withers
> > If this was:
> >
> > Method
> >   | answer i |
> >   answer := Array new: 5.
> >   i := 1.
> >   answer at: i put: [:arg | arg * i]].
> >   i := 2.
> >   answer at: i put: [:arg | arg * i]].
> >   i := 3.
> >   answer at: i put: [:arg | arg * i]].
> >   i := 4.
> >   answer at: i put: [:arg | arg * i]].
> >   i := 5.
> >   answer at: i put: [:arg | arg * i]].
> >   ^answer
> >
> > Then all five blocks would have i = 5.  Is it so?
> 
> Yes, and this is what is essentially going on in Squeak.

Here is an SUnit test case for the two examples going around.  The one
for Disjoint Environment Blocks which only work with #fixTemps and one
for Sync Environment Blocks which only work without Fixtemps.  I am
actually testing all four options.   It is real messy and not very clear
what is being tested.  This is my first SUnit test so I'll claim
inexperience.  :)

Here are the tests and their outcomes.

The #testDisjointEnvironmentBlocksWithFixTemps     Succeeds.
The #testDisjointEnvironmentBlocksWithoutFixTemps  Fails.
  | answer |
  answer := Array new: 5.
  1 to: 5 do:
    [:i |
    answer at: i put: [:arg | arg * i] fixTemps].
  ^answer



The #testSyncEnvironmentBlocksWithFixTemps         Fails.
The #testSyncEnvironmentBlocksWithoutFixTemps      Succeeds.
   | foo |
   ^Array
     with: [:arg | foo := arg]
     with: [foo]


Apologies for not distributing clearer code.  Perhaps this can help us,
with cleanup, into the future

-Rob


> --Vassili

-- 
--------------------------------------------------
Smalltalking by choice.  Isn't it nice to have one!
-------------- next part --------------
'From Squeak2.7 of 5 January 2000 [latest update: #1762] on 31 January 2000 at 6:06:17 pm'!
TestCase subclass: #TestClosureCase
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Closure-Test'!

!TestClosureCase reorganize!
('running' constructDisjointEnvironmentBlocksWithFixTemps constructDisjointEnvironmentBlocksWithoutFixTemps constructSyncEnvironmentBlocksWithFixTemps constructSyncEnvironmentBlocksWithoutFixTemps setUp testDisjointEnvironmentWithFixTemps testDisjointEnvironmentWithoutFixTemps testSyncEnvironmentWithFixTemps testSyncEnvironmentWithoutFixTemps)
!


!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 17:39'!
constructDisjointEnvironmentBlocksWithFixTemps

	|array| 
	array := Array new: 5.
	1 to: 5 do:
		[:i |
		array at: i put: [ i] fixTemps].
	^array
! !

!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 17:39'!
constructDisjointEnvironmentBlocksWithoutFixTemps

	|array| 
	array := Array new: 5.
	1 to: 5 do:
		[:i |
		array at: i put: [ i] ].
	^array
! !

!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 17:42'!
constructSyncEnvironmentBlocksWithFixTemps

	|array result| 
	result := 10.
	array := Array new: 2.
	array at: 1 put: [:arg | result := arg] fixTemps.
	array at: 2 put: [result] fixTemps.
	^array
! !

!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 18:02'!
constructSyncEnvironmentBlocksWithoutFixTemps

	|array result| 
	result := 10.
	array := Array new: 2.
	array at: 1 put: [:arg | result := arg].
	array at: 2 put: [result].
	^array
! !

!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 17:49'!
setUp
	"Sets up the fixture. This method is called before a test is executed."
! !

!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 17:39'!
testDisjointEnvironmentWithFixTemps
	|blockArray|
	blockArray := self constructDisjointEnvironmentBlocksWithFixTemps.
	self should: [|isCorrectValue|
		isCorrectValue := true.
		1 to: 5 do: [:i | isCorrectValue := isCorrectValue & ( (blockArray at: i) value == i)].
		isCorrectValue].
	! !

!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 17:56'!
testDisjointEnvironmentWithoutFixTemps
	|blockArray|
	blockArray := self constructDisjointEnvironmentBlocksWithoutFixTemps.
	self should: [|isCorrectValue|
		isCorrectValue := true.
		1 to: 5 do: [:i | isCorrectValue := isCorrectValue & ( (blockArray at: i) value == i)].
		isCorrectValue].
	! !

!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 17:56'!
testSyncEnvironmentWithFixTemps
	|blockArray| 
	blockArray := self constructSyncEnvironmentBlocksWithFixTemps.
	self should: [
		(blockArray at: 1) value: 5.
		(blockArray at: 2) value == 5].
	! !

!TestClosureCase methodsFor: 'running' stamp: 'rww 1/31/2000 17:46'!
testSyncEnvironmentWithoutFixTemps
	|blockArray|
	blockArray := self constructSyncEnvironmentBlocksWithoutFixTemps.
	self should: [
		(blockArray at: 1) value: 5.
		(blockArray at: 2) value == 5].
! !




More information about the Squeak-dev mailing list