Code Blocks

Pupeno pupeno at pupeno.com
Tue Aug 31 13:44:00 PDT 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tuesday August 31 2004 13:29, Pupeno wrote:
> In the manual, in section 2.1.1 Code Blocks:
> http://slate.tunes.org/doc/progman/node7.html#SECTION00041100000000000000
> it says:
> "Blocks furthermore have the property that, although they are a piece of
> code and the values they access may change between defining the closure and
> invoking it, the code will ``remember'' what objects it depends on,
> regardless of what context it may be passed to as a slot value. It is
> called a lexical closure since it ``closes over'' the environment and
> variables used in its definition, the lexical context where it was born."
> Can anybody help me understand this ? what does the code block "remember" ?
> Thanks.
Answering to myself... as I was asked too since this was discussed in the irc 
channel...
Code blocks are executed like if they where executed in the place where they 
are created. So, if there's another slot 'at the same level but outside' of 
the code block, the code block can access it even when it's run from another 
place (like if we send the code block to a method that run it). This can be 
show by an example (some outputs removed for clarity):
Slate: Growing heap to 4726380 bytes.
Hi, there!
Slate 1> addSlot: #test valued: 'This is a string inside the slot test'. 
"Creates a slot called test containing 'test'"
Slate 2> addSlot: #printTest valued: [Console ; test. ]. "Creates a slot 
called printTest that will print what's inside test"
Slate 3> printTest do. "Print what's inside test"
This is a string inside the slot testNil
Note: the Nil at the end is because printTest returns Nil and the 
'interpreter' prints it.
Slate 4> 1 = 1 ifTrue: printTest. "Passing printTest to another method, it 
still access test from the environment where it was created"
This is a string inside the slot testNil
Slate 5> test: 'A different string'. "Storing a different string in test"
'A different string'
Slate 6> 1 = 1 ifTrue: printTest. "The test being accessed is, well, test"
A different stringNil

Some of the things I show in the example may seem stupid or ovious to lots of 
people here, but for newcomers from legacy languages, it's important. Maybe 
an example like this could be added to the manual ?
- -- 
Pupeno: pupeno at pupeno.com - http://www.pupeno.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBNOMSfW48a9PWGkURArxOAJ9Psn7BTz/zCCj3wgftFCGsXPIx7gCfU1gN
QnEb70L9ji+DJReJ0t5gV2Q=
=ysTa
-----END PGP SIGNATURE-----




More information about the Slate mailing list