6

I am implementing futures in Pharo. I came across this website http://onsmalltalk.com/smalltalk-concurrency-playing-with-futures. I am following this example and trying to replicate it on Pharo. However, I get to this point the last step and I have no idea what ">>" means: This symbol is not also included as part of Smalltalk syntax in http://rigaux.org/language-study/syntax-across-languages-per-language/Smalltalk.html.

BlockClosure>>future
    ^ SFuture new value: self fixTemps

I can see future is not a variable or a method implemented by BlockClosure. What should I do with this part of the code to make the promises/futures work as indicated at http://onsmalltalk.com/smalltalk-concurrency-playing-with-futures? I cannot add it on the Playground or as a method to my Promise class as it is, or am I missing something?

After adding the future method to BlockClosure, this is the code I try on the PlayGround.

value1 := [200 timesRepeat:[Transcript show: '.']. 6] future.
value2 := [200 timesRepeat:[Transcript show: '+']. 6] future.
Transcript show: 'other work'.
Transcript show: (value1 + value2).
Date today 

The transcript displays the below error instead of the expected value of 12.

UndefinedObject>>DoIt (value1 is Undeclared) 

UndefinedObject>>DoIt (value2 is Undeclared) 
Gakuo
  • 845
  • 6
  • 26

1 Answers1

10

For some reason that it would be nice to learn, there is a traditional notation in Smalltalk to refer to the method with selector, say, m in class C which is C>>m. For example, BlockClosure>>future denotes the method of BlockClosure with selector #future. Interestingly enough, the expression is not an evaluable Smalltalk one, meaning, it is not a Smalltalk expression. It is just a succinct way of saying, "what comes below is the source code of method m in class C". Just that.

In Smalltalk, however, methods are objects too. In fact, they are instances of CompiledMethod. This means that they can be retrieved by sending a message. In this case, the message is methodAt:. The receiver of the message is the class which implements the method and the argument is the selector (respectively, C and #m, or BlockClosure and #future in your example).

Most dialects, therefore, implement a synonym of methodAt: named >>. This is easily done in this way:

>> aSymbol
  ^self methodAt: aSymbol

This puts the Smalltalk syntax much closer to the traditional notation because now BlockClosure>>future looks like the expression that would send the message >> to BlockClosure with argument future. However, future is not a Symbol unless we prepend it with #, namely #future. So, if we prefix the selector with the # sign, we get the literal Symbol #future, which is a valid Smalltalk object. Now the expression

BlockClosure >> #future

becomes a message, and its result after evaluating it, the CompiledMethod with selector #future in the class BlockClosure.

In sum, BlockClosure>>future is a notation, not a valid Smalltalk expression. However, by tweaking it to be BlockClosure >> #future, it becomes an evaluable expression of the language that returns the method the notation referred to.

eMMe
  • 569
  • 5
  • 16
Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51
  • I added the future method to BlockClosure. However, it seems that "^ SFuture new value: self fixTemps" does not work as fixTemps message is "send but not implemented". I looked at this website https://www.gnu.org/software/smalltalk/manual-base/gst-base.html. This is what it says about fixTemps: fixTemps:- This should fix the values of the temporary variables used in the block that are ordinarily shared with the method in which the block is defined. Not defined yet, but it is not harmful that it isn’t. Answer the receiver. It states "Not defined yet". How can I address this issue? – Gakuo Apr 22 '18 at 10:15
  • 1
    @Gakuo You don't need `fixTemps` in Pharo. Just send `self` as the argument. – Leandro Caniglia Apr 22 '18 at 12:41
  • After doing away with fixTemps, I tried to run the test code given on the website. I get an error: "UndefinedObject>>DoIt (value1 is Undeclared)" and "UndefinedObject>>DoIt (value2 is Undeclared)". I expected this code to work as indicated on the website http://onsmalltalk.com/smalltalk-concurrency-playing-with-futures – Gakuo Apr 22 '18 at 15:45
  • @Gakuo Variables `value1` and `value2` are defined as instances of `SFuture`, so if you evaluate those expressions, these variables will be declared. The fact that you are getting them as undeclared means that you haven't evaluated these assignments. At least, this is the only I can imagine without seeing what you are doing. – Leandro Caniglia Apr 22 '18 at 18:51
  • I believe it has to be some other reason. I execute each line at a time (ctrl -D ) and also try to execute the entire test all at once. The debugger pops up and the the Transcript displays "UndefinedObject>>DoIt (value1 is Undeclared)" and "UndefinedObject>>DoIt (value2 is Undeclared)" for both cases. – Gakuo Apr 22 '18 at 18:58
  • @Gakuo Try a Playground instead of the Transcript. – Leandro Caniglia Apr 22 '18 at 20:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169545/discussion-between-gakuo-and-leandro-caniglia). – Gakuo Apr 22 '18 at 21:08
  • Your help has been highly appreciated. I have further questions at:https://stackoverflow.com/questions/50071106/creating-a-key-value-message-in-smalltalk-pharo-that-take-blocks-as-argument – Gakuo Apr 27 '18 at 22:44