Today we're going to review something else you've already learned
to really bring it home.
The topic is the difference between literal and dynamic arrays.
What you will learn is that:
Literal arrays are not created by sending messages,
dynamic messages are created at runtime using messages,
but both are instances of the array class.
They're simply two methods for creating arrays.
If you remember,
to create a literal array, we use the syntax #(
and in the middle, I can put any object in textual form.
In this case, the integer 45, the string 'milou',
the number 1300, the boolean true,
and the symbol #tintin.
If I ask this literal object for its class,
the return value is an instance of Array.
The dynamic version
of this literal array, is right here.
So to create a dynamic array, I take the array class,
I create an instance using the method "with-with-with-with".
I send it with all these values
and it creates an array class instance equal to the one above.
It's just two different ways of creating objects.
This is another version of the dynamic array.
I could have created an array manually
by sending the message "new" to the array class.
Then I would have used "at" and "put" to fill in the array,
and then return the array.
This is another way to create a dynamic array.
But we also have a specific syntax called syntactic sugar,
which is made with braces.
Okay?
These braces enable me to write exactly what I wrote above,
only faster and more concisely.
But it's the same thing.
This means, I will use braces
and put a set of expressions separated by dots,
which will be evaluated to create the collection.
The big difference
between a literal array
and an array created dynamically with braces...
If I take the expression...
Here's an example: I take a variable and initialize it at 12.
I want to create an array. Since it's a literal array, I use #(
and I add 'a + 1. 13'
and it returns this array.
It's an array that will contain the symbols a and +,
integer 1, symbol . and integer 13.
I do the same thing here with a dynamic array.
a = 12
braces a + 1. 13
and I get an array with 2 elements:
13 and 13.
Why?
Because a + 1 was evaluated as an expression.
a = 12 + 1. 13
Each expression separated by a dot was evaluated
before creating an array.
So the important difference is right here:
When I use braces, it executes expressions,
however when I use #( to create a literal array,
the literal expressions are not executed.
Why not?
Because it's the compiler that will create the array
in the case of a literal array.
Here's another somewhat more complicated example.
I start with #(
and I reuse parentheses inside. So this is one point.
And I reuse parentheses to produce a nested literal array.
We see that nothing was interpreted in this literal array,
because it was created at compile time by the compiler.
So we have one array that contains nested arrays.
You can see them here.
The first nested array contains 10,
the symbol @, the integer 20, etc.
To give you proof: If I ask for element 1 of this array,
I get an array.
Whenever I put parentheses in a literal array,
it creates nested arrays.
What's important to remember
is that we have one single type of array in Pharo:
The array class and its instances.
But there are various ways to obtain instances and create arrays.
The first way is literal syntax: #(
Be careful, arrays are created by the compiler at compile time.
We have the simple dynamic form: Array new.
I send the message "new to the array class.
And we have another more syntactically compact dynamic method
with braces before and after, and expressions separated by dots
that are evaluated to create an array.