[英]Where do undeclared variables go in Smalltalk?
例子:
st> [ fnord := 7 ] value
我一直以為他們進入了Smalltalk
的 SystemDictionary ,但事實並非如此:
st> [ fnord := 7 ] value
st> Smalltalk at: #fnord
Object: SystemDictionary new: 512 "<0x2acfca382030>" error: Invalid argument #fnord: key not found
但是,至少在 GNU Smalltalk 上,這些值似乎fnord
在某處——訪問fnord
返回正確的值:
st> [ fnord := 7 ] value
st> fnord
7
更新:我想出了如何拆卸積木! 這真的很難。
st> [ fnord := 7 ] block inspect
An instance of CompiledBlock
header: 32768
clean-ness flags: 0
number of arguments: 0
number of temporaries: 0
number of literals: 4
needed stack slots: 8
method: UndefinedObject>>executeStatements
literals: [
[1] {fnord}
[2] a BlockClosure
[3] #block
[4] #inspect
]
byte codes: [
[1] source code line number 1
[3] push 7
[5] store into Global Variable {fnord}
[7] pop stack top
push Global Variable {fnord}
[9] return stack top
]
[] in UndefinedObject>>executeStatements
所以它肯定認為它正在寫入一個全局變量。
未聲明的變量綁定進入名為Undeclared
的全局字典。 一旦你正確地聲明它,這個綁定(一個key->value
對)就會被移到Smalltalk
。 例如,這就是在加載代碼時解析前向引用的方式。 也就是說,在加載聲明它的代碼之前使用變量時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.