4

Smalltalk syntax (and features) can be found pretty exotic (and even disturbing) when you come from a more C-like syntax world. I found myself losing time with some
I would be interested in learning knowing what you found really exotic compared to more classic/mainstream languages and that you think helps to understand the language.

For example, evaluation with logic operators :

  • (object1 = object2) & (object3 = object4) : this will evaluate the whole expression, even if the left part is false, the rest will be evaluated.
  • (object1 = object2) and: [object3 = object4] : this will evaluate the left part, and only will evaluate the right part if the first is true.
Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
Serty Oan
  • 1,737
  • 12
  • 19
  • 1
    How is any of this exotic? Think in terms of sending messages. You're in a room full of people, so am I; I want to ask you a question, so I open my mouth and start saying words. You receive these words, and react accordingly. Either by ignoring them, responding to them or performing some bodily expression to communicate a response. It's actually more foundational than exotic when you stop and think about it how it's intended. – jer Jun 26 '10 at 18:56
  • As I said, when I say "exotic", it means "exotic compared to classic/mainstream C/Java-like language". I agree that message sending makes SmallTalk closer to human thinking than other languages, but we (programmers, or at least me) are formatted to think in a C world. That was just my point. – Serty Oan Jun 26 '10 at 19:06
  • Coming from a Delphi background, I'm used to boolean expressions that might or might not be evaluated in either fashion, based on a compiler switch! – Frank Shearar Jun 26 '10 at 19:34
  • about & and `and`, they seems not exotic, but logical to me; and I have a C/asm background – ShinTakezou Jul 06 '10 at 20:54
  • I agree with `&`: it behaves exactly the same as it does in C/Java. The "exotic" thing here is that you send the `and:` message (with a code block as an argument) to the result of the comparison. – Joachim Sauer Sep 01 '10 at 12:44
  • @joachim: but an `&&` operator that behaves the same as in C/Java would require a specific rule to evaluate its right operand lazily. Using a keyword message with a block argument makes it possible to define the lazy semantics while keeping Smalltalk semantics uniform and minimalistic. – Damien Pollet Dec 07 '12 at 22:09

5 Answers5

10

Everything is an object, and everything above the VM's available for inspection and modification. (Primitives are part of the VM, conceptually at least.) Even your call stack's available (thisContext) - Seaside implemented continuations back in the day by simply swizzling down the call stack into a stream, and restoring it (returning to the continuation) by simply reading out activation frames from that stream!

You can construct a selector from a string and turn it into a Symbol and send it as a message: self perform: 'this', 'That' will do the same thing as self thisThat. (But don't do this, for the same reasons you should avoid eval in both Lisps and PHP: very hard to debug!)

Message passing: it's not method invocation!

#become: is probably a bit of a shock to anyone who hasn't seen it before. (tl;dr a wholesale swapping of two object pointers - all references to B now point to A, and all references to A now point to B)

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
4

How about selective breakpointing (which I actually use at times):

foo
    thisContext sender selector == #bar ifTrue:[ self halt ].
    ...

will debug itself, but only if called from bar. Useful, if foo is called for from zillion other places and a regular breakpoint hits too often.

blabla999
  • 3,130
  • 22
  • 24
4

Primitves

someMethod
  <primitive 14122 wtf>
  "fail and execute the following"
  [self] inlineCopyInject: [:t1 | self].
Richard Durr
  • 3,081
  • 3
  • 20
  • 26
4

My first wrestling session with Smalltalk was the metaclass implementation.

Consider this:

What is the class of 'This is a string'? Well, something like String.

What is the class of String? String class. Note: this is a class, but it has no name, it just prints itself as 'String class'.

What is the class of String class? Metaclass. Note: this is a named class.

What is the class of Metaclass? As you might expect (or not) this is Metaclass class. Of which, again as you might expect, the class is Metaclass again.

This is the first circularity. Another one which I found rather esoteric at first (of course, now I eat metaclasses for breakfast) is the next one:

What is the superclass of String? Object (eventually, different implementations of Smalltalk have different class hierarchies of these basic classes).

What is the superclass of Object? nil. Now this is an interesting answer in Smalltalk, because it actually is an object! nil class answers UndefinedObject. Of which the superclass is ... Object.

Navigating through the superclass and instance of relations was a real rollercoster ride for me in those days...

robject
  • 808
  • 1
  • 6
  • 12
  • In Squeak, `Object` subclasses `ProtoObject`, which subclasses `nil`. `nil` is an `UndefinedObject`, subclassing `Object`. Your point still stands though :) – Frank Shearar Sep 01 '10 at 13:30
3

I've always been fond of the Smalltalk quine:

quine
     ^thisContext method getSource

(Pharo version.)

Ash Wilson
  • 22,820
  • 3
  • 34
  • 45