Pharo 3中文字和动态数组之间的区别

时间:2021-10-12 13:16:47

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

阅读Pharo(Pharo By Example)的文档,第一个区别在于数组的制作方式。

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 ?

这两者之间还有其他区别吗?如果动态数组可以执行文字数组的操作,为什么存在文字数组?

2 个解决方案

#1


6  

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

动态数组如{1 + 2。 4 - 2。 3}基本上是一个语法糖:

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.

文字数组实际上是文字的,并在编译时定义。

#2


4  

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.

文字数组是标准的Smalltalk语法,动态数组是Squeak(因此也是Pharo)扩展。我相信其他Smalltalks中存在类似的语法,但它并不普遍。

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.

所以文字数组存在的原因是因为它们总是有 - 它们是Smalltalk 80的一部分。除了语法和它们被评估时,我认为没有任何其他差异 - 我相信它们都会产生一个对象同一类型,只是它们的初始化方式不同。

#1


6  

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

动态数组如{1 + 2。 4 - 2。 3}基本上是一个语法糖:

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.

文字数组实际上是文字的,并在编译时定义。

#2


4  

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.

文字数组是标准的Smalltalk语法,动态数组是Squeak(因此也是Pharo)扩展。我相信其他Smalltalks中存在类似的语法,但它并不普遍。

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.

所以文字数组存在的原因是因为它们总是有 - 它们是Smalltalk 80的一部分。除了语法和它们被评估时,我认为没有任何其他差异 - 我相信它们都会产生一个对象同一类型,只是它们的初始化方式不同。