What is a simple and concise way to break from a whileTrue
loop in GNU Smalltalk that doesn't require returning?
Here is my code. I want to break from the loop at Line 31 at the end if char_stack
is empty.
https://gist.github.com/SYZYGY-DEV333/ea3f5eeb3473927c8faa294bb72a8858
Any help would be much appreciated.
One of the articles of the Byte magazine (1982) titled Building Control Structures in the Smalltalk-80 System by Peter Deutsch , shows how easy is to implement while-loop breaks for infrequent events that might happen inside the loop.
To implement this we only need a new class and an extension to
BlockClosure
, making a total of 9 lines of code(!).The class:
BlockWithExit
, subclass ofObject
with two ivarsexit
andblock
and the following methodsExtension
and that's it!
Example
Find the max of a collection until its exhaustion or until
nil
is found (the infrequent event)Why does this work the way it does? Because a block with a non-local return exits from the method that defines the block.
In this case this method is
BlockWithExit>>value
, therefore when[^nil]
is evaluated fromloop exit
, the flow exitsvalue
and goes to its sender, right afterloop value
.The outstanding corollary of Deutsch's discovery is that the whole mechanism of
Exceptions
can be built using this very same trick of defining an exit block in an ivar like:exit := [^nil]
.