[Newbies]  whileTrue:  implementation
erlis at erlisvidal.com
Tue Oct 4 16:41:34 UTC 2011
Thanks for your answer, it clarifies some issues but I'm still confused
about the logic I've found in the whileTrue method.
For example, I don't know the answers to the following questions:
Can I add methods to literal blocks? Where do I implement those methods? In
case your answer is in BlockClosures, why I don't see any ifTrue: in the
Thanks once again
On Tue, Oct 4, 2011 at 12:22 PM, Bert Freudenberg <bert at freudenbergs.de>wrote:
> 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
> ^ [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: ].
> fac value: 10
> This would not have worked with contexts.
> - Bert -
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners