Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of ">>" in Pharo/Smalltalk

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) 
like image 544
Gakuo Avatar asked Apr 21 '18 22:04

Gakuo


People also ask

What is Pharo used for?

Pharo is used for natural language processing. Pharo is used for machine learning and neural network processing. Smalltalk, in general, is versatile. The U.S. joint military used Smalltalk to write a million-line battle simulator called JWARS.

What is Pharo language?

Pharo is a pure object-oriented programming language and a powerful environment, focused on simplicity and immediate feedback (think IDE and OS rolled into one). Previous Next. Discover. Learn more about Pharo's key features and elegant design.


1 Answers

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.

like image 159
Leandro Caniglia Avatar answered Oct 17 '22 20:10

Leandro Caniglia