Home > Articles > Programming

  • Print
  • + Share This
From the author of

Turtles All the Way Down

In an apocryphal story, a physicist is giving a talk about the solar system. (Exactly which physicist depends on who is telling the story.) At the end of the talk, a woman stands up and says, "What you have told us is rubbish. The world is really a flat plate supported on the back of a giant turtle." The physicist asks what the turtle is standing on, to which the accuser replies, "You're very clever, young man, but it's turtles all the way down!"

The phrase "turtles all the way down" is often used by Smalltalk developers to refer to the fact that everything is an object, and to refer to other "everything is an x" languages. This representational simplicity has inspired a lot of other languages.

Smalltalk, like Lisp, showed that it was possible to achieve good performance without the need for explicit primitive types. JavaScript inherits this principle, as do a lot of other "scripting" languages, although few do it in such a clean way as Smalltalk. In Smalltalk, small integers were instances of the SmallInt class. The virtual machine typically has a special case for these integers, because they're so common.

Real objects are all identified by pointer values, which must be word-aligned, meaning that the lowest bit is always zero. Smalltalk virtual machines set the lowest bit for SmallInt "pointers" to 1 and then use the remaining 31 (or 63) bits to store the value. This plan avoids the need to allocate space for SmallInt instances; they're stored inside their pointer. When a calculation returns a value that can't be stored in a SmallInt, it's promoted to a BigInt object, which encapsulates an arbitrary-sized integer and is referenced by a pointer, just like any other object. The message-send machinery lets you treat these two interchangeably. Additionally, you can define the same messages (+, -, and so on) that the standard integer types implement in your own objects, and provide another numeric type that can be used anywhere that numbers are valid.

  • + Share This
  • 🔖 Save To Your Account