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
- Every object answers
classwith its own class. respondsTo: #sqrtasks before sending.isKindOf:climbs the superclass chain for you.
Keywords and builtins used here
CollectionNumberOrderedCollectionTranscriptclasscrnamenewprintString
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.
Step 1 of 3 in Reflection, step 26 of 31 in Language basics.