[Newbies] [] whileTrue: [] implementation

Bert Freudenberg bert at freudenbergs.de
Tue Oct 4 16:22:57 UTC 2011

On 04.10.2011, at 17:58, Erlis Vidal wrote:

> Hi all, 
> I was looking at the implementation of some of the flow control methods and I have a question with the method whileTrue. 
> First of all, I can see two identical implementation in the classes BlockClosure and BlockContext the implementation is this 
> whileTrue: aBlock 
>     "Ordinarily compiled in-line, and therefore not overridable.
>     This is in case the message is sent to other than a literal block.
>     Evaluate the argument, aBlock, as long as the value of the receiver is true."
>     ^ [self value] whileTrue: [aBlock value]
> I'm assuming here that there's another class Block I'm missing (something like the literal block mentioned in the comment) which is the one that contains the logic I was looking for. But I'm not able to find any other whileTrue method in my image.

"Block" is just short for either BlockClosure or BlockContext. "Literal" blocks are those written directly with square brackets. If you store a block in a variable and pass that variable, the block would not be literal.

> What's the difference between BlockClosure and BlockContext? 

BlockClosures are BlockContexts Done Right.

If you wrote square brackets in older Squeak versions (3.x) you would get a BlockContext. In a current Squeak you get a BlockClosure.

So since now we only have closures, the difference is only of historic interest. You can do some things with closures that you couldn't do with block contexts, e.g. recursive blocks:

| fac |
fac := nil.
fac := [:n | n > 1 ifTrue: [n * (fac value: n - 1)] ifFalse: [1]].
fac value: 10

This would not have worked with contexts.

- Bert -

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20111004/ff50e16c/attachment.htm

More information about the Beginners mailing list