Aha! How to get rid of Blocks in Smalltalk.
Lex Spoon
lex at cc.gatech.edu
Sat Apr 22 11:30:10 UTC 2000
Dean_Swan at Mitel.COM wrote:
>
>
> This is not just an abstract example (ok, well this particular snippet is a
> "toy" problem, but I'm not ready to discuss my *real* application publicly).
> This code actually works, and I can't think of a "cleaner" way to have
> implemented a general purpose framework for genetic algorithms. I have other
> methods which use my genetic algorithm framework, where the evaluation block
> references other instance variables from the context in which the block was
> created also. How would you accomplish this without blocks? Even if it can be
> done (which it probably can), it would involve defining additional classes, and
> probably be a lot harder to read.
>
Well, to be fair, most of your examples are to:do: loops. The part that
isn't, the evaluationBlock stuff, could actually be changed to a regular
method pretty easily. Just define a class with an
evaluate:inPopulation: method. In fact, this might even be *easier* to
read, because 7-8 lines of code will end up in a named method in a named
class.
Putting the block in a method would also give you the flexibility to
have parameterized evaluators.
But still, the basic to:do: examples stand. For such basic control
structures, the extra abilities of access to the surrounding local
variables and non-local return are clearly critical. I guess you could
say that blocks need extra abilities because they are used to implement
control structures.
-Lex
More information about the Squeak-dev
mailing list
|