nil in Smalltalk
An object like any other; ifNil:ifNotNil: branches on it.
"nil is an object too; ifNil:ifNotNil: replaces null checks."
| proofreader assistant |
proofreader := 'Alba'.
assistant := nil.
proofreader
ifNil: [Transcript show: 'Nobody reading proofs'; cr]
ifNotNil: [:who | Transcript show: who, ' reads proofs'; cr].
assistant
ifNil: [Transcript show: 'No assistant on shift'; cr].
Transcript show: (assistant isNil) printString; cr.
Transcript show: (assistant ifNil: ['(unassigned)']) ; cr.
How it works
ifNotNil:passes the non-nil value into its block.ifNil:supplies the default for the missing case.isNilanswers the plain boolean question.
Keywords and builtins used here
TranscriptassistantcrisNilnilprintStringproofreader
The run, in numbers
- Lines
- 14
- Characters to type
- 435
- Tokens
- 77
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 77 seconds.
Step 3 of 3 in Control flow, step 9 of 31 in Language basics.