这段代码:

((1 to: 10)
    inject: (WriteStream on: String new)
    into: [ :strm :each |
        ((each rem: 3) = 0)
            ifTrue: [
                strm
                    nextPutAll: each printString;
                    space;
                    yourself ]]) contents


失败,因为在strm块中使用ifTrue:的位置未定义。为什么在那里看不到它?

编辑:我在VASt和Pharo中进行了尝试。

最佳答案

问题是隐式ifFalse:分支返回nil。要解决此问题,请尝试以下操作:

((1 to: 10)
    inject: (WriteStream on: String new)
    into: [ :strm :each |
        ((each rem: 3) = 0)
            ifFalse: [strm]  "This is needed to avoid nil being returned"
            ifTrue: [
                strm
                    nextPutAll: each printString;
                    space;
                    yourself ]]) contents

07-25 23:13