我不知道您是否只是在询问错误,或者您是否正在尝试在代码的基础上实现其他目标。由于代码分散且不完整,我无法给你一个正确的答案,但我会改编一个类似于你的代码,它似乎可以工作:
|data|
data:= Dictionary new.
('To'= 'To') ifTrue: [data at:#To put:'someone@mail.com'].
('To'= 'Another thing') ifTrue: [data at:#To put:'shouldnt appear'].
data at:#From put:'me@mail.com'.
message:= String streamContents:
[:stream|
data values
do:[:each| stream nextPutAll: each ]
separatedBy: [ stream nextPut: Character space ]].
如果您将其复制到工作区并检查其data
内容,它将为您提供以下ByteString
'someone@mail.com me@mail.com'
. 但我认为你已经完成了这项任务。您能否详细说明您正在寻找的内容并在您编写代码时放置完整的代码?
根据 OP 说明进行编辑
Ok, now I see the problem. The thing is that in Smalltalk you don't have "out" parameters and that is why the compiler is telling you that it can't store new contents in the message
parameter. From the point of view of the object sending the message, you see that message send as telling an object to perform an action and passing the required collaborators to that object to perform it. If there is a response from the message send it is expected to be in the return value. I re-wrote your method a little bit here to do that:
addHeader: headerName email: aString message: aMessage
| data newValues |
data:= Dictionary new.
'To'= headerName ifTrue: [data at:#To put: aString.].
'From'= headerName ifTrue: [data at: #From put: aString].
'subject'= headerName ifTrue: [data at: #subject put: aString].
"adding dicitonay values to message"
newValues := String streamContents:[:stream|
data values do:[:each| stream nextPutAll: each ]
separatedBy: [ stream nextPut: Character space ]].
^aMessage , newValues.
as you can see the idea is to use the #,
message to join the two strings and return the new value. Some things to note here are:
- I changed a little bit the names to use a more accepted capitalization pattern.
- You may want to take a look at the
#asSymbol
message to avoid the if statements.
HTH