Tob*_*ias 6 smalltalk squeak pharo
一些样式指南和习语表明你不应该改变文字数组,就像在这种情况下:
MyClass>>incrementedNumbers
    | numbers |
    numbers := #( 1 2 3 4 5 6 7 8 ).
    1 to: numbers size: do: [:index |
        numbers at: index put: (numbers at: index) + 1].
    ^ numbers
我为什么不这样做?
注意:以下是依赖于实现的.在ANSI Smalltalk的标准定义:
未指定相同文字的值是相同还是不同的对象.还未指定特定文字的单独评估的值是相同还是不同的对象.
也就是说,你不能依赖两个(相等的)文字是相同的或不同的.但是,以下是一种常见的实现方式
至少在Squeak和Pharo中,在保存(=编译)方法时构造文字数组并存储在方法对象(a CompiledMethod)中.这意味着更改文字数组会更改存储在方法对象中的值.例如:
MyClass>>example1
    | literalArray |
    literalArray := #( true ).
    literalArray first ifTrue: [
       literalArray at: 1 put: false.
       ^ 1].
    ^ 2
此方法1仅在第一次调用时返回:
| o p |
o := MyClass new.
o example1. "==> 1"
o example1. "==> 2"
o example1. "==> 2"
p := MyClass new.
p example1. "==> 2"
这甚至独立于接收器.
但同样,你不能依赖它,它可能与其他Smalltalks不同.
复制(始终安全)
为了克服这个问题,您可以在使用前简单地复制文字数组.你的例子:
MyClass>>incrementedNumbers
    | numbers |
    numbers := #( 1 2 3 4 5 6 7 8 ) copy. "<====== "
    1 to: numbers size: do: [:index |
        numbers at: index put: (numbers at: index) + 1].
    ^ numbers
这总是安全的,不会改变方法对象中的数组.
支撑数组(大多数是可移植的)
虽然未在标准中定义,但大多数实现都支持这样的支撑数组表达式:
{ 1 . 'foo' . 2 + 3 }. 
这相当于:
Array with: 1 with: 'foo' with: 2 + 3.
这些数组是在执行时构造的(与文字数组相反),因此可以安全使用.再次举例:
MyClass>>incrementedNumbers
    | numbers |
    numbers := { 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 }. "<====== "
    1 to: numbers size: do: [:index |
        numbers at: index put: (numbers at: index) + 1].
    ^ numbers
实际上有时会有理由改变文字数组(或者更常见的是任何方法文字,坦率地说).例如,如果您有静态信息,如图像或二进制数据,它们根本不会更改但不会一直使用,但您不能(无论出于何种原因)使用实例或类变量,您可以将对象存储在文字数组中首次使用时:
MyClass>>staticInformation
    | holder |
    holder := #( nil ).
    holder first ifNil: [ holder at: 1 put: self generateBinaryData ].
    ^ holder first
该ifNil:检查将只真正的第一次执行的方法,后续执行将只返回被返回的值self generateBinaryData在第一次调用期间.
一些框架暂时使用了这种模式.但是,特别是对于二进制数据,大多数Smalltalks(包括Squeak和Pharo)现在支持表单的文字字节数组#[ … ].然后可以简单地将该方法写成
MyClass>>staticInformation
    ^ #[42 22 4 33 4 33 11 4 33 0 0 0 0 
        4 33 18 4 33 4 33 9 0 14 4 33 4 
        33 7 4 33 0 0 9 0 7 0 0 4 33 10
        4 33 4 33 7 4 33 0 0 9 0 7 0 0 4 
        " ... "
        33 10 4 33 4 33 17 0 11 0 0 4 33
        4 33 0 0 17 0 7 0 0 4 33 13 0]