Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

Everything Is an Object in Smalltalk: Numbers, Booleans, nil, Classes

See why Smalltalk has no primitives: integers, fractions, booleans, nil, blocks and even classes are objects. Learn class, isKindOf:, respondsTo: and the object hierarchy from Object down.

In many languages, numbers and booleans are "primitives" that live outside the object system, and a few special keywords control the program. Smalltalk removes the exceptions. A number is an object, true is an object, nil is an object, a block of code is an object and a class is an object. Learning to ask objects about themselves is the key skill of this lesson.

All Smalltalk code here is read-only; JavaScript stand-ins let you experiment with the same questions.

Asking an object what it is

Every object understands class:

SMALLTALK
3 class.                 "-> SmallInteger"
(2 raisedTo: 100) class. "-> LargePositiveInteger"
(1/3) class.             "-> Fraction"
3.14 class.              "-> Float (BoxedFloat64 or SmallFloat64)"
'text' class.            "-> ByteString"
#sym class.              "-> ByteSymbol"
$a class.                "-> Character"
true class.              "-> True"
nil class.               "-> UndefinedObject"
[ 1 + 1 ] class.         "-> FullBlockClosure"
Object class.            "-> Object class"

The exact names can differ slightly between versions (floats and strings have several internal representations), but the idea is stable. Notice that true is the only instance of class True, and nil is the only instance of UndefinedObject. Both are normal objects with methods.

Numbers form a tower

Smalltalk numbers automatically convert up when needed:

SMALLTALK
1/3 + (2/3).        "-> 1  (exact: Fraction arithmetic)"
(1/3) asFloat.      "-> 0.3333333333333333"
1000 factorial printString size.   "-> 2568 digits, no overflow"
5 even.             "-> false"
10 gcd: 4.          "-> 2"
-7 abs.             "-> 7"
17 \\ 5.            "-> 2 (modulo)"
17 // 5.            "-> 3 (integer division)"

SmallInteger fits in a machine word; when a result is too big the system quietly switches to LargePositiveInteger. The dividing messages \\ and // are binary selectors made of two characters; / on integers returns a Fraction, never a truncated result.

Booleans and control are messages

SMALLTALK
3 > 2.                         "-> true"
(3 > 2) & (1 > 5).             "-> false"
(3 > 2) and: [ 1 > 5 ].        "-> false (the block is only run if needed)"
(3 > 2) ifTrue: [ 'yes' ] ifFalse: [ 'no' ].   "-> 'yes'"

ifTrue:ifFalse: is a message sent to a Boolean with two block arguments. The True class implements it by evaluating the first block; False evaluates the second. No if statement exists in the grammar. (The compiler inlines these common messages for speed, but the semantics are those of message sending.)

nil is an object too

SMALLTALK
nil isNil.                  "-> true"
nil ifNil: [ 'empty' ].     "-> 'empty'"
nil printString.            "-> 'nil'"
nil foo.                    "-> error: UndefinedObject does not understand #foo"

An unassigned variable holds nil. Sending an unknown message to it is the Smalltalk equivalent of a null-pointer exception, which is why you will see the idiom ifNil: and ifNotNil: often.

Blocks and classes are objects

SMALLTALK
| add |
add := [ :a :b | a + b ].
add value: 3 value: 4.      "-> 7"
add numArgs.                "-> 2"
Integer superclass.         "-> Number"
Integer name.               "-> 'Integer'"
Integer selectors size.     "-> a number: how many methods the class defines"

A block is an object you can store, pass and call later with value. A class is an object you can ask for its superclass, its name and its methods. The system that is running your program is built from the same objects as your program.

The hierarchy at a glance

flowchart TD
    PO["ProtoObject"] --> O["Object"]
    O --> M["Magnitude"]
    O --> B["Boolean"]
    O --> C["Collection"]
    O --> U["UndefinedObject"]
    M --> N["Number"]
    M --> CH["Character"]
    N --> I["Integer"]
    N --> F["Float"]
    N --> FR["Fraction"]
    B --> T["True"]
    B --> FA["False"]
    C --> SC["SequenceableCollection"]

A small slice of the tree. Everything inherits from Object (via ProtoObject), so every object understands messages like class, printString, isNil and ==.

Questions every object can answer

SMALLTALK
3 isKindOf: Number.            "-> true (instance of Number or a subclass)"
3 isMemberOf: SmallInteger.    "-> true (exactly that class)"
3 respondsTo: #factorial.      "-> true"
3 respondsTo: #foo.            "-> false"
3 isInteger.                   "-> true"
'a' isString.                  "-> true"
3 = 3.0.                       "-> true (equal values)"
3 == 3.                        "-> true (the same object; small integers are unique)"
'abc' == 'abc'.                "-> false (two different string objects)"

Know the difference between = (equal value) and == (identical object). Compare strings with =.

JS Stand-in: ask an object about itself

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: booleans as objects with ifTrue:ifFalse:

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the class of the object true?

    true is the only instance of the class True, a subclass of Boolean. False is the only instance of False.
  2. 2. What is the difference between = and == ?

    'abc' = 'abc' is true, but 'abc' == 'abc' is false because they are two distinct string objects.
  3. 3. What does nil foo do?

    nil is a normal object. It only understands the messages defined for UndefinedObject and Object, so other messages open the debugger.
  4. 4. Which message asks whether an object can answer a given selector?

    respondsTo: takes a symbol such as #factorial and returns true if the receiver understands it.

Summary

  • There are no primitives: numbers, booleans, nil, blocks and classes are all objects.
  • class, isKindOf:, respondsTo: and isNil let you ask an object about itself.
  • Numbers form a tower (SmallInteger, LargeInteger, Fraction, Float) with exact arithmetic by default.
  • ifTrue:ifFalse: and and: are messages sent to Booleans with block arguments.
  • Use = for equal values and == for identity.

Next: Smalltalk Syntax on a Postcard