问题
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)
回答1:
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.
来源:https://stackoverflow.com/questions/49960789/the-use-of-in-pharo-smalltalk