typestar

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

  1. ifNotNil: passes the non-nil value into its block.
  2. ifNil: supplies the default for the missing case.
  3. isNil answers the plain boolean question.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Control flow, step 9 of 31 in Language basics.

← Previous Next →