Dynamic Vs. Literal Arrays
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
What You Will Learn
- Literal arrays are not created using messages
- Dynamic arrays are created at runtime using messages
- Still they are all instances of
Array
Remember: Literal Arrays
Literal array definition can only contain objects that have a textual (literal) representation: numbers, strings, nil, symbols, boolean
Literal arrays are instances of the class Array
Literal/Dynamic Arrays
A literal array
A dynamic array
Both are Array
instances
Dynamic Array Compact Syntax
Defining dynamic arrays is tedious
{ expression1 . expression2 }
is syntactic sugar to create dynamic arrays
Literal Array Creation Time
Literal arrays are created at compile time by the parser when the expression is read and not during the execution
Dynamic arrays execute expressions
Literal vs. Dynamic
{}
executes expressions while #()
not
Nested Arrays
()
inside a literal array produces a nested literal array
Summary
- Only one kind of Arrays
- Three ways
- Literal syntax:
#( )
(no message)
- Using messages
Array new: 3
- Syntactic sugar: Dynamic
{ . . }
/