Smalltalk – How WhileTrue: Works

smalltalk

I want to know how whileTrue: works. I searched the implementation that is in BlockClosure:

whileTrue: aBlock 
    ^ [self value] whileTrue: [aBlock value]

And another implementation with no parameter:

whileTrue
    ^ [self value] whileTrue: []

But I don't know how that works, that's a recursive method, but this led me to ask several questions:

  1. How does this recursive call terminate?
  2. If [self value] returns a Boolean object, why is whileTrue: not implemented in the Boolean type?
  3. Why is there another implementation named whileTrue that just do not receive any block and evaluates self?

Best Answer

whileTrue: is inlined by the compiler, that's why you don't see the actual implementation. Here's the comment out of Pharo:

"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."

If you look at the byte code of #whileTrue: you'll see that the compiler simply uses a jump to make the loop:

17 <70> self
18 <C9> send: value
19 <9C> jumpFalse: 25
20 <10> pushTemp: 0
21 <C9> send: value
22 <87> pop
23 <A3 F8> jumpTo: 17 <---- here's the jump
25 <7B> return: nil

#whileTrue is also inlined directly (slightly optimized byte code). The inlining is also the reason why there's no implementation of this on Boolean (apart from the fact that there's no point in reevaluating a boolean...).

#whileTrue is there so that you can easily create endless loops the only terminate e.g. when the process terminates, a semaphore is signaled, an exception occurs etc.

Related Topic