[Q] Is a vertical bar necessary in block?

Peter William Lount peter at smalltalk.org
Sat May 15 20:33:26 UTC 1999


Stefan Matthias Aust wrote:
> why not also allow anonymous parameters in 
> normal keyword method, like
>
> doSomething: a : b
>  "..."
>
> which is then called as
>
>  self doSomething: 1 : 2

Objective-C, which is based on Smalltalk, does exactly that, but why? It's
very cryptic and when I encounter something like "length: :" I almost
always have to look up what the parameters are in the source code or
documentation. This "look up" is almost always required for blocks (that I
didn't write and that I remember the parameters for). A well named keyword
parameter can assist the programmer/user in knowing how to use the method
without "looking up" the parameters in the documentation. It would be cool
to bring this capability to blocks. How?

Variations on the block "syntax sugar theme"
[:a :b | a < b] Normal Block
[:a :b a < b] Without vertical bar
[: a : b a < b] Like a keyword method
[: a : b | c | c := a * b. c] Like a keyword method with a temporary
variable.

"Potential Cool Solution
Why not name the parameters just like a keyword method? "
aBlock := [length: a width: b | area | area := a * b. area].

"or without the temporary variable"
aBlock := [length: a width: b  a * b].

"Like a keyword method but in block format (with the only real syntax
difference being '[' and ']' surrounding the code). A single method object
(which is what a block is) with no permanant (but temporary) variables!
Activated via "
	anArea := aBlock length: 100 width: 50. "Easy to understand the
parameters..."

"instead of:"
	anArea := aBlock value: 100 value: 50. "What parameter is value: and
value: ?"

"Which is easier to understand? I would suggest that the second one is
easier to understand and use."

"(This block keyword usage would make copying the code into a 'real method'
in a class almost trivial...just copy, paste and run...)."

"Would you still want the blocks to be accessed by value: and value:value:
and ...?"

"In prototype languages this keyword parameter usage would be very
interesting... start with a block with a single 'method' and start adding
on 'instance variables' and additional 'methods' and you get a full blown
object! Another interesting way to define an object..."

"In design discussions I had with Diana Merry-Shapiro, one of the original
Smalltalk team members, she consistently questioned the use of 'blocks with
parameters'. Why don't you make a full object instead even if it only has a
single method? Indeed why not have a "single method object class" instead?
Class methods are great for this? Our example then becomes:"

!AreaCalc methods!
length: a width: b
	^ a * b

"and is invoked by:"
	anArea := AreaCalc length: 100 width: 50.

"looks just like our example above in terms of usage?"
	anArea := aBlock length: 100 width: 50.

In essense a single method class becomes a block defined with a global
variable (AreaCalc in this case). What's the difference? Local v.s. Global
scoping? Easier to write a single method block when you need it? 

Obviously one essential difference is that blocks are Smalltalk's means of
implementing 'flow control' within a program. Without blocks you'd need to
define methods within methods for each ifTrue: and ifFalse: statement...
yick...

How do you decide when to use a 'block with a parameter' v.s. building a
class with one or more methods?

Does anyone know the early history regarding the creation of blocks? Where
did they come from? How was the current form determined? (Dan, Alan?)

Peter William Lount
peter at smalltalk.org
http://www.smalltalk.org

p.s. To answer your question in the subject of this message, the vertical
bar seems to be 'sugar'. It tastes (or looks) good but you gain a few extra
calories (or bytes) from using it.





More information about the Squeak-dev mailing list