typestar

Strings and symbols in Smalltalk

Strings are collections; symbols are interned names.

"Strings are collections; symbols are unique interned names."
| title |
title := 'letterpress broadside'.

Transcript show: title asUppercase; cr.
Transcript show: (title copyReplaceAll: 'broad' with: 'bed'); cr.
Transcript show: (title indexOf: $b) printString; cr.
Transcript show: title reversed; cr.

"A symbol equals itself everywhere; strings only by content."
Transcript show: (#pressRun == #pressRun) printString; cr.
Transcript show: ('run' = 'run') printString; cr.
Transcript show: #broadside printString; cr.

How it works

  1. copyReplaceAll:with: rewrites; reversed flips.
  2. indexOf: $b finds a character's position.
  3. Symbols compare with == — one object per name.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
520
Tokens
75
Three-star pace
60 tpm

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

Type this snippet

Step 1 of 3 in Strings, step 18 of 31 in Language basics.

← Previous Next →