Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Literal and Dynamic arrays in Pharo 3

Reading the documentation of Pharo (Pharo By Example) the first difference is in the way that arrays are made.

A literal will follow this syntax

myArray := #(1 2 3)

while a dynamic array with

myArray := {1+2 . 4-2 . 3 }

A literal array will take values directly , containing numbers, strings and booleans. While a dynamic array will take full messages that will compile and insert their returning values to the array.

Is there are any other difference between the two ? Why do literal arrays exist if dynamic arrays can do what literal arrays do ?

like image 866
Kilon Avatar asked May 15 '14 08:05

Kilon


2 Answers

Dynamic array like { 1 + 2 . 4 - 2 . 3 } is basically a syntactic sugar for:

Array
  with: 1 + 2;
  with: 4 - 2;
  with: 3

Which makes sense because arrays are created quite often. Also you can incorporate this to create a dictionary for example:

{
  #keyOne   -> 5 .
  #keyTwo   -> 3 .
  #keyThree -> 1
} asDictionary

Literal arrays as actually literal and are defined before compile time.

like image 95
Uko Avatar answered Nov 05 '22 08:11

Uko


Literal arrays are standard Smalltalk syntax, dynamic arrays are a Squeak (and therefore Pharo) extension. I believe similar syntax exists in other Smalltalks, but it's not universal.

So the reason literal arrays exist is because they always have - they're part of Smalltalk 80. Other than syntax and when they're evaluated, I don't think there's any other difference - I believe they both result in an object of the same type, it's only how they're initialized that's different.

like image 24
Stuart Herring Avatar answered Nov 05 '22 09:11

Stuart Herring