Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!Germany.EU.net!EU.net!Austria.EU.net!siemens.co.at!news
From: szol@garwein.hai.siemens.co.at (Andreas Szolarz)
Subject: Re: passing variables into blocks
Sender: news@siemens.co.at (Newssoftware)
Message-ID: <1995Mar22.160100.17259@siemens.co.at>
Date: Wed, 22 Mar 1995 16:01:00 GMT
Reply-To: szol@garwein.hai.siemens.co.at
References: <D5qnGH.5JC@festival.ed.ac.uk>
Nntp-Posting-Host: garwein.hai.siemens-austria
Organization: Siemens AG Austria EZE TNA1
Lines: 62

In article <D5qnGH.5JC@festival.ed.ac.uk>, olly@festival.ed.ac.uk (O Morgan) writes:
> Sorry, another newbie question.
> 
> In ST (Digitalk for Windows, version 2), is it possible to bind a
> variable to a block variable.  For instance, if I wanted to pass a block
> of code to a method, and instantiate the block variable to a specific
> method variable.  Eg:  I pass  [ c| (c > 3) and: [c < 8 ] ] as a
> parameter to method test:, below.  The question is, is there anything
> equivalent to 'do:' which could 'iterate' an object.  I know I could put
> the object into a collection first, but I'd like to do it directly.
> 
> test: aBlock
>      | someVariable |
>       someVariable:= 1.  
>       ^someVariable doObject: aBlock
> 
> Thanks for any help.
> 
> PS can anyone recommend a good book for ST/V, I've reached the stage
> where I need more than the Digitalk tutorial.
> 
> Thanks for any help.
> 
> 
> Olly Morgan




Supposed your method should answer true/false (I don`t understand what
you could 'iterate'), then this works:

	test: aBlock
 	     | someVariable |
  	    someVariable:= 1.  
  	    ^aBlock value: someVariable
  	    
If you send 'value: aValue' to a Block then it is evaluated with parameter 'aValue':
      
    	[ :c | (c > 3) and: [c < 8 ] ] value: 1           ---> false


If you definitely want to implement a 'doObject:' method (perhaps better
called 'evaluateBlock:'), that any object can respond to, 
implement it in class Object:
 

	Object>doObject: aBlock
	
		^aBlock value: self



Hope this helps & best regards,
Andreas


_________________________________________________________________
Andreas Szolarz             email: szol@garwein.hai.siemens.co.at
Siemens AG Austria


