[Q] Is a vertical bar necessary in block?

Reinier van Loon R.L.J.M.W.van.Loon at inter.nl.net
Sat May 15 12:12:45 UTC 1999


Folks,

I was wondering why the vertical bar is necessary in a block.

I consider a block to be an unnamed method where each colon in a block
denotes a parameter just as in a normal method.

[ : a : b ] could be viewed as a method with selector :: (colon-colon).

Just as in a method the vertical bar is not necessary, this bar could be
dropped from the block syntax.

I patched the parser (see below) to ignore the absence of a vertical bar and
as far as I can see things still compile correctly.

So, my first question is: Is there any (compelling) reason for the vertical
bar in the block syntax?

My second question is: Why not name the arguments in a block?
Okay, might be a bit of overkill, but why not? It's much closer to the
method syntax and there's one less rule to learn.
E.g.

( 1 to: 10 ) inject: 0 into:

    previous: total current: each        "named block arguments"
        total := total + each.
].

And going on to block closures might not be difficult anymore ( if we parse
a block as a method local to its enclosing method ).
E.g.

( 1 to: 10 ) inject: 0 into:

    previous: total current: each
        | temp1 temp2 |
        total := total + each.
].

Yes, I know it's an ANSI deviation, but simplification is also important.
And there is always a next version of the standard...

Reinier.

The patch to Parser

Parse>>blockExpression
 " [ {:var} ( | statements) ] => BlockNode."

 | argNodes |
 argNodes _ OrderedCollection new.
 [self match: #colon
 "gather any arguments"]
  whileTrue:
   [argNodes addLast: (encoder autoBind: self argumentName)].
 (argNodes size > 0 & (hereType ~~ #rightBracket) and: [(self match:
#verticalBar) not])
  ifTrue: ["^self expected: 'Vertical bar'"].                "RvL: ignore
the absence of a vertical bar"
 self statements: argNodes innerBlock: true.
 (self match: #rightBracket)
  ifFalse: [^self expected: 'Period or right bracket'].
 argNodes do: [:arg | arg scope: -1] "Scope no longer active"





More information about the Squeak-dev mailing list