About whileTrue:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

About whileTrue:

stéphane ducasse-2
Hi

one person asked in the squeak-fr mailing-list the following question  
that I could not deeply answer.
So I would like to learn :)

Here is the question:
In a book it is written that whileTrue: is implemented like that (I  
guess that this is in VW)

  BlockClosure>>whileTrue: aBlock
   ^ self value
       ifTrue:
          [ aBlock value.
          self whileTrue: aBlock ]

While in Squeak it is (I checked up to 3.5 to see).

  BlockClosure>>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]

So normally this code should not work, because of nicely recursive at  
several levels.

Now I read what VW said:

whileTrue: aBlock
        "Evaluate the argument, aBlock, as long as the value
        of the receiver is true."

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

        "This method is inlined if both the receiver and the argument are  
literal
        blocks. In all other cases, the code above is run. Note that the code
        above is defined recursively. However, to avoid actually building an
        activation record each time this method is invoked recursively, we have
        used the '[...] whileTrue: [..]' form in the last line, rather than  
the more
        concise 'self whileTrue: aBlock'. Using literal blocks for both the  
receiver
        and the argument allows the compiler to inline #whileTrue:, which  
(in the
        absence of type inferencing) could not be done if we were to use
        'self whileTrue: aBlock'."

Now my question:

it seems that the code of whileTrue: should not be run at all  
contrary to the
one of VisualWorks "This method is inlined if both the receiver and  
the argument are literal
blocks. In all other cases, the code above is run.".
I understand that in VW
        self value
                ifTrue:
at least allows to escape the endless loops.
Else if the code is run how does it get rid of the block creation  
[self value].

Stef


Reply | Threaded
Open this post in threaded view
|

Re: About whileTrue:

Avi  Bryant

On Feb 19, 2006, at 1:34 AM, stéphane ducasse wrote:

>
> Now my question:
>
> it seems that the code of whileTrue: should not be run at all  
> contrary to the
> one of VisualWorks "This method is inlined if both the receiver and  
> the argument are literal
> blocks. In all other cases, the code above is run.".
> I understand that in VW
> self value
> ifTrue:
> at least allows to escape the endless loops.
> Else if the code is run how does it get rid of the block creation  
> [self value].

Because [self value] is a literal block (as is [aBlock value]), and  
so the #whileTrue: send inside the #whileTrue: implementation does  
get inlined, and there's no recursion.

Avi