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:
- How does this recursive call terminate?
- If
[self value]
returns aBoolean
object, why iswhileTrue:
not implemented in theBoolean
type? - Why is there another implementation named
whileTrue
that just do not receive any block and evaluatesself
?
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:If you look at the byte code of
#whileTrue:
you'll see that the compiler simply uses a jump to make the loop:#whileTrue
is also inlined directly (slightly optimized byte code). The inlining is also the reason why there's no implementation of this onBoolean
(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.