Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
53 views1 page

69 PharoByExample

Blocks provide a mechanism to defer the execution of expressions. A block is an anonymous function with a definition context. A block is executed by sending it the message value, which returns the value of the last expression in its body, unless there is an explicit return. Blocks can take parameters declared with a colon and separated from the body by a vertical bar. The number of parameters determines which value message to send, up to 4 arguments. More than 4 requires valueWithArguments and passing arguments in an array. Blocks can also declare local variables surrounded by vertical bars.

Uploaded by

lop1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views1 page

69 PharoByExample

Blocks provide a mechanism to defer the execution of expressions. A block is an anonymous function with a definition context. A block is executed by sending it the message value, which returns the value of the last expression in its body, unless there is an explicit return. Blocks can take parameters declared with a colon and separated from the body by a vertical bar. The number of parameters determines which value message to send, up to 4 arguments. More than 4 requires valueWithArguments and passing arguments in an array. Blocks can also declare local variables surrounded by vertical bars.

Uploaded by

lop1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

4.

5 Block syntax

1. the method pattern, containing the name (i.e., lineCount) and any
arguments (none in this example)
2. comments (these may occur anywhere, but the convention is to put one
at the top that explains what the method does)
3. declarations of local variables (i.e., cr and count); and
4. any number of expressions separated by dots (here there are four)
The execution of any expression preceded by a ^ (a caret or upper arrow,
which is Shift-6 for most keyboards) will cause the method to exit at that
point, returning the value of that expression. A method that terminates
without explicitly returning some expression will implicitly return self.
Arguments and local variables should always start with lower case letters.
Names starting with upper-case letters are assumed to be global variables.
Class names, like Character, for example, are simply global variables referring
to the object representing that class.

4.5 Block syntax


Blocks (lexical closures) provide a mechanism to defer the execution of expres-
sions. A block is essentially an anonymous function with a definition context.
A block is executed by sending it the message value. The block answers the
value of the last expression in its body, unless there is an explicit return (with
^), in which case it does not answer any value.
[ 1 + 2 ] value --> 3

Blocks may take parameters, each of which is declared with a leading colon. A
vertical bar separates the parameter declaration(s) from the body of the block.
To evaluate a block with one parameter, you must send it the message value:
with one argument. A two-parameter block must be sent value:value:, and
so on, up to 4 arguments.
[ :x | 1 + x ] value: 2 --> 3
[ :x :y | x + y ] value: 1 value: 2 --> 3

If you have a block with more than four parameters, you must use valueWith-
Arguments: and pass the arguments in an array. (A block with a large number
of parameters is often a sign of a design problem.)
Blocks may also declare local variables, which are surrounded by vertical bars,
just like local variable declarations in a method. Locals are declared after any
arguments:
[ :x :y |
| z |
z := x + y.
z ] value: 1 value: 2 --> 3

63

You might also like