Is it really all about message passing in Smalltalk?
Asked Answered
E

2

15

I'm new to Smalltalk and I'm impressed with the fact that there are only just 6 keywords in the language (self, super, true, false, nil & thisContext), and how pure it is in having almost everything as message passing, eg. looping using whileTrue, if/else using ifTrue, etc ... which are way different from what I'm used to in other languages.

Yet, there are cases where I just cannot make sense of how message passing really fit in, these include:

  • the assignment operator :=
  • the cascading operator ;
  • the period operator .
  • the way to create a set #( ... )

These aren't message passing, right?

Electrostatics answered 12/4, 2011 at 15:50 Comment(3)
+1 but how are these typically used? To me "message passing" is about how objects interacts with each other. If you're talking about details inside objects, to me it's unrelated and the point is moot: en.wikipedia.org/wiki/Message_passing Note that even in a totally "non-pure OO language" like Java you can do better "message passing" than what the average Java dev will do (for example getters and setters everywhere, not realizing getters and setters are the anti-thesis of OO).Bluecollar
Yup, some of them really aren't message passing. But then i was thinking := looks like one, eg. x := 1 + 2 may just mean that send the colon equals message to x, such that it takes the value of 1+2, but then, the precedence rule doesn't fit in.Electrostatics
@Bluecollar "getters and setters are the anti-thesis of OO" Quite the reverse, surely? Simple instance accessors defined in the object's own class are the basis of OO. They are the foundation-stone of encapsulation, which is what allows for loosely-coupled objects. Many Java coders must be incorporating complex and tightly-coupled behaviours into the basic getter and setter accessor methods if getter and setters are viewed as the antithesis of OO in Java code. Or am I missing something?Allineallis
E
13

As you've discovered, there's still some actual Smalltalk syntax. Block construction, literal strings/symbols/comments, local variable declaration (|...|), and returning (^) are a few things you didn't mention which are also syntax.

Some extensions (e.g. #(...), which typically creates an Array, not a set) are certainly expressible otherwise, for example #(1 2 3) is equivalent to Array with: 1 with: 2 with: 3; they're just there to make the code easier to read and write.

Elman answered 12/4, 2011 at 16:3 Comment(2)
Note that the return ^ and the assignment := are also expressible using message sends: thisContext return: anObject and anObject instVarNamed: 'foo' put: anObject (for instance variables, temporaries and globals work similar).Carducci
We can almost say that ^ and := are syntactic sugar ;)Gombroon
A
2

One thing that might help clarify : self, super, true, false, nil & thisContext are data primitives, rather than keywords.

They are the only 6 data primitives. These 6 are also known as pseudo-variables. Absolutely every other thing is an instance of Class Object or its subclasses.

There are very few pre-defined keywords in Smalltalk. They can be written in a very condensed form.

A famous example is Smalltalk Syntax on a Postcard (link)

 exampleWithNumber: x

    | y |
    true & false not & (nil isNil) ifFalse: [self halt].
    y := self size + super size.
    #($a #a "a" 1 1.0)
        do: [ :each |
            Transcript show: (each class name);
                       show: ' '].
    ^x < y

Here's the comment for this method - which is larger than the method itself:

"A method that illustrates every part of Smalltalk method syntax except primitives. It has unary, binary, and keyboard messages, declares arguments and temporaries, accesses a global variable (but not an instance variable), uses literals (array, character, symbol, string, integer, float), uses the pseudo variables true, false, nil, self, and super, and has sequence, assignment, return and cascade. It has both zero argument and one argument blocks."

Allineallis answered 16/11, 2015 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.