Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

":=" and binary message precedence in Smalltalk

I am attempting to learn Smalltalk through the tutorials included with Dolphin Smalltalk X6.

My question deals with the evaluation of expressions with multiple messages.

My understanding is that simple messages are evaluated first, then binary messages, and finally keyword messages (with the exception of code in parenthesis). However, I am having trouble applying this understanding to the second line in the following example (found in the Dolphin Smalltalk tutorial).

    playground := Playground new.
    teresa := playground add: Triangle new.       "Misunderstood code"
    teresa class.                                 "Evaluates to 'Triangle'"

If my understanding were correct, the 2nd line would be evaluated thusly:

    1. Simple message 'new' sent to Triangle, triangle object as response
    2. Binary message ':=' with parameter 'playground' sent to 'teresa'.
    3. Keyword message 'add:' with parameter 'triangle object' sent to 'teresa'.
    4. teresa class. "evaluates to 'Playground'".

My misunderstanding is in how 'teresa' comes to refer to the anonymous Triangle object and not the Playground object referred to by 'playground'.

I have looked up a second explanation of Smalltalk evaluation for mentions of := or add: being special cases with no success, and the only other explanation I can think of is a fundamental misunderstanding.

Any help straightening me out?

like image 396
failboat_cpt Avatar asked Dec 07 '22 09:12

failboat_cpt


1 Answers

The Assignment operator (:=) isn't a message. (It's not sent to an object, instead it indicates that a variable should be set to a value). Its precedence is last overall.

So what happens is:

  1. Simple message 'new' send to Triangle, triangle object as response
  2. keyword message add: sent to playground, and there's a convention that add: messages answer the object added, which this one seems to follow, so the newly created triangle is returned.
  3. The variable teresa is set to the new triangle
  4. teresa class. "evaluates to Triangle".
like image 182
Stuart Herring Avatar answered Dec 29 '22 07:12

Stuart Herring