Category:Smalltalk: Difference between revisions

Content added Content deleted
Line 367: Line 367:
====Block Arguments and Locals====
====Block Arguments and Locals====
This are visible inside a block and all of its enclosed blocks. Args are specified by listing them prefixed by a colon, following a vertical bar, followed by optional local variables:
This are visible inside a block and all of its enclosed blocks. Args are specified by listing them prefixed by a colon, following a vertical bar, followed by optional local variables:
<lang smalltalk>[:arg | ... code here...]. "a one-arg block"
<lang smalltalk>[:arg | ... code here...]. "a one-arg block"
[:a1 :a2 :a3 | ... ] "a three-arg block"
[:a1 :a2 :a3 | ... ] "a three-arg block"
[ |local1 local2| ... ] "a no-arg block with 2 locals"
[ |local1 local2| ... ] "a no-arg block with 2 locals"
[:a :b :c | | l1 l2 l3 | "3 args (a,b,c) and three locals (l1,l2,l3)"</lang>
[:a :b :c | | l1 l2 l3 | "3 args (a,b,c) and three locals (l1,l2,l3)"</lang>
Blocks can be nested, and inner blocks can refer to any statically visible outer scope's variable.
Blocks can be nested, and inner blocks can refer to any statically visible outer scope's variable.