help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] How to exit loops?


From: Paolo Bonzini
Subject: Re: [Help-smalltalk] How to exit loops?
Date: Thu, 24 Jul 2008 10:35:51 -0700 (PDT)


Lukas Renggli wrote:
> 
> My preferred way is the following:
> 
> Add the following method to Object (or somewhere else):
> 
> Object>>escaper: aBlock
>    ^ aBlock value: [ ^ nil ]
> 
> Then implement your loop like this:
> 
> self escaper: [ :break |
>    1 to: 10 do: [ :x |
>       x = 3 ifTrue: [ break value ].
>       ... ] ]
> 

If you don't care about performance, you can use "Continuation currentDo:"
instead of "self escaper:". :-)

As an additional bonus, continuations support both 0- and 1-argument value,
so that you can return a value if you wish.

Paolo
-- 
View this message in context: 
http://www.nabble.com/How-to-exit-loops--tp18056234p18636867.html
Sent from the Gnu - Smalltalk mailing list archive at Nabble.com.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]