Friday 15 March 2013

smalltalk - initialization of local block variable only at the first time the user call the block -



smalltalk - initialization of local block variable only at the first time the user call the block -

is there way write block ( not parameters) @ first call.

i want initialize local block variable @ first time , alter value each time user phone call block : block value.

my block defined in method within class b. , method returns block.

so each time phone call method should initialization. every time phone call block should go on same point.

for example: want initialized 0.

a ^[i:=i+1] ob1 = b new. res= obj1 a. transcript show: res value. // should print 1 transcript show: res value. // should print 2 res2= obj1 a. transcript show: res2 value. // should print 1

here's modified code snippet.

a | first | first := true. ^[first iftrue: [i := 0. first := false]. := i+1]

or more simply:

a | | := 0. ^[i := i+1]

technically sec illustration initializes variable before block executed. if that's ok, works. if want variable initialized on first call, utilize first example.

smalltalk squeak

No comments:

Post a Comment