Search code examples
arraysdynamicsmalltalkliteralspharo

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 ?


Solution

  • 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.