compiling blocks
John Brant
brant at cs.uiuc.edu
Tue Apr 28 23:32:42 UTC 1998
At 04:36 PM 4/28/98 -0300, David Farber wrote:
>
>how do i compile '[ :foo | foo bar]' on the fly so that i can store it in an
>instance variable and send value: to it sometime later on?
>
>also, what if i reference self in the block, like '[ :foo | foo bar: self]'.
>what do i need to do to make sure that self evaluates to the object that is
>holding the block?
You can look at the Compiler>>evaluate:in:to:notifying:ifFail: method.
Here's an example:
Integer>>foo
| test block |
test := 5.
block := Compiler new
evaluate: '[:a | a + test + self]'
in: thisContext
to: self
notifying: nil
ifFail: [nil].
^block value: 10
4 foo ==> 19 (a=10, test=5, self=4)
John Brant
More information about the Squeak-dev
mailing list
|