Best Future ever

Best Future ever

I’m attending some classes in University to finish up my degree. The classes in itself are close to an abuse and punishment (they are mandantory) but on the commute I manage to read one paper in each direction. I was reading Reflective Facilities in Smalltalk-80 and saw the best implementation of a Future ever. In their Smalltalk-80 dialect all message sends go through a #dispatchMessage: selector and the future is implemented in there.

dispatchMessage: aMessage
semaphore wait.
result become: self
^super dispatchMessage: aMessage

This implementation waits for a signal in the semaphore and then turns itself into the result of the operation. This allows one to write code like this
| f |
f := Future promising: [2.0 + 2.0].
f printString.
The execution of the message sent to the Future will block in the #dispatchMessage: until the result of 2.0 + 2.0 is available and then send the printString to the result. There is only one downside, most smalltalk dialects don’t have a dispatchMessage: like this.
Comments are closed.