Variables
Variables in Beamtalk are simple: assign with :=, use the name, and they
go out of scope at the end of the method or script. No type declarations,
no var/let/const keywords.
Assignment with :=
:= is the assignment operator. The name on the left, the value on the right.
x := 42 // => _
x // => 42
greeting := "Hello, World!" // => _
greeting // => Hello, World!
Variables hold any type — there are no type declarations:
result := 3 + 4 // => _
result // => 7
Reassignment is allowed:
count := 1 // => _
count := count + 1 // => _
count // => 2
No declarations needed
Unlike Smalltalk (which requires | var1 var2 | declarations at the top of
a method), Beamtalk introduces variables at the point of first assignment.
This just works — no declaration block needed:
a := 10 // => _
b := 20 // => _
a + b // => 30
Multiple assignments
Assign multiple variables separately, then combine them:
first := 1 // => _
second := 2 // => _
third := 3 // => _
first + second + third // => 6
Destructuring assignment
Arrays can be destructured into multiple variables in one step:
#[x, y] := #[10, 20] // => _
x // => 10
y // => 20
Works with any expression that evaluates to an array:
#[a, b, c] := #[1, 2, 3] // => _
a + b + c // => 6
Variables in expressions
Variables are just names for values. Use them anywhere a value is expected:
width := 5 // => _
height := 3 // => _
area := width * height // => _
area // => 15
String interpolation uses variables directly:
name := "Beamtalk" // => _
"Welcome to {name}!" // => Welcome to Beamtalk!
Instance variables (slots) — a preview
Inside a class definition, instance variables are accessed via self.name.
This chapter covers only local (temporary) variables.
Chapter 10 (Value Classes) and Chapter 11 (Actors) cover instance variables.
The key distinction:
x := 42— local variable, lives in the current method/scriptself.x := 42— slot assignment on an object; valid forActor subclass:andObject subclass:, but rejected at compile time forValue subclass:(values are immutable)
For now, all variables we've seen are local.
Naming conventions
- Variable names:
lowerCamelCase—count,firstName,totalPrice - Class names:
UpperCamelCase—Integer,String,Counter,ShoppingCart - Symbols used as constants:
#lowerCamelCaseor#UPPER_SNAKE(both seen in stdlib)
Beamtalk is case-sensitive:
myVar := "hello" // => _
myVar // => hello
Summary
x := expr assign (introduce or rebind)
x read
#[a, b] := arr destructure
Exercises
1. Swap two variables. Assign x := 10 and y := 20, then swap their values
using a temporary variable so that x is 20 and y is 10.
Hint
x := 10
y := 20
tmp := x
x := y
y := tmp
// x is now 20, y is now 10
2. Destructuring with wildcards. Given #[a, _, c] := #[10, 20, 30], what
are the values of a and c? What happens to the middle element?
Hint
a is 10, c is 30. The _ wildcard matches and discards the value 20 —
it is not bound to any variable.
3. Rectangle calculations. Create variables width and height, then compute
both the area (width * height) and perimeter (2 * (width + height)) and store
each in its own variable.
Hint
width := 8
height := 5
area := width * height // 40
perimeter := 2 * (width + height) // 26
Next: Chapter 4 — Messages (the fundamental Beamtalk concept)