Smalltalk is a real dynamic object system (with the language just built in) and unique in the world of computing.
While a function/method name in a class has to be unique so it could be called - it usually is a Symbol. You know all these #foo, #bar, #negated, ...
In Java you may call it foo() or bar() or negated().
But in Smalltalk this selector could be ANY object. Yes, yes - this is not a typo.
It could be any object, just try it. For instance with the integer 1:
It could be any object, just try it. For instance with the integer 1:
|selectorThatCouldBeAnObject existingMethod|
selectorThatCouldBeAnObject := 1.
existingMethod := EllipseMorph methodDict at: #heading.
EllipseMorph methodDict at: selectorThatCouldBeAnObject put: existingMethod.
EllipseMorph new perform: selectorThatCouldBeAnObject
So the message could even be the object that is receiving the message/itself as message:
| existingMethod receiver|
existingMethod := EllipseMorph methodDict at: #heading.
receiver := EllipseMorph new.
EllipseMorph methodDict at: receiver put: existingMethod.
receiver perform: receiver
Cool !!!
Alan Kay once said that "Smalltalk is object-oriented, but it should have been message oriented."
And as we now know a message is an object and an object could be a message. Mhhh - have to think more on this...
Excellent!
ReplyDeleteI always discover some new fun possibilites in Smalltalk ;-)
Thanks for sharing that.
Luc