typestar

Asking objects in Smalltalk

class, respondsTo:, isKindOf:: reflection as conversation.

"Ask any object what it is: class, respondsTo:, isKindOf:."
| things |
things := OrderedCollection new.
things add: 42; add: 'galley'; add: #proof; add: 3/4.

things do: [:each |
    Transcript show: each printString, ' is a ',
        each class name; cr].

Transcript show: (42 respondsTo: #sqrt) printString; cr.
Transcript show: ('galley' isKindOf: Collection) printString; cr.
Transcript show: (42 isKindOf: Number) printString; cr.

How it works

  1. Every object answers class with its own class.
  2. respondsTo: #sqrt asks before sending.
  3. isKindOf: climbs the superclass chain for you.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
425
Tokens
77
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 77 seconds.

Type this snippet

Step 1 of 3 in Reflection, step 26 of 31 in Language basics.

← Previous Next →