Вопрос

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 ?

Это было полезно?

Решение

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.

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top