I have some "progress" feedback in the application I'm working on. It ticks off steps as the application starts up. The steps all different durations. Many are slow enough, you can see them go by. They're at a fine enough resolution, that it feels like you're making comfortable progress. But there's a couple that are so fast, they just fly by. So, I'd like to actually ensure that each step takes at least some amount of time. Even if it can really finish faster.
What's so cool about Smalltalk that it has this wonderfully terse definition of a closure, just put code between [ ]'s. I know of no other language that makes it so easy.But what's even cooler is that you can add your own methods to them, to create new forms of execution. Since the message for evaluating a block is value, we'll just add our own valueInAtLeast: aSeconds, implemented as such:
valueInAtLeast: aSeconds
| delay |
delay := Delay untilMicroseconds: Time microsecondClock + (aSeconds * 1000000) rounded.
^self ensure: delay wait
Now we can wrap any arbitrary chunk of code in a block and send valueInAtLeast: to it and be guaranteed it will take that long to execute.
[3 + 4] valueInAtLeast: 1
will compute the classical first Smalltalk expression, but make sure it takes at least 1 second to do it. All those years of making Smalltalk execute faster, and now someone makes it take longer.