简体   繁体   English

在Smalltalk中,如果x是一个数组,并且x at:3 put:123将起作用,那么(x at:3)+ 1如何工作?

[英]In Smalltalk, if x is an array, and x at: 3 put: 123 will work, then how can (x at: 3) + 1 work?

That is, if we look at it as all objects and messages, then 也就是说,如果我们将其视为所有对象和消息,那么

"Did this earlier:    x := Array new: 20"
x at: 3 put: 123      "this will put 123 into slot 3"

can work only if x at: 3 return like a "cell" object, so the cell can take in an Interger object, which is 123. (or, so that the cell object can let some myContent property point to the 123 object) 只有当x at: 3返回时才能像“单元格”对象一样工作,所以单元格可以接收一个Interger对象,即123.(或者,单元对象可以让一些myContent属性指向123对象)

Then in this case, how can 那么在这种情况下,怎么可能

y := (x at: 3) + 567

work? 工作? Because how does a cell deal with the + message? 因为单元格如何处理+消息? Is it that somehow the cell object thinks it doesn't know how to handle the + message, so it looks into its content (maybe by something like self myContent ) and then return it? 是不是以某种方式,单元格对象认为它不知道如何处理+消息,所以它查看其内容(可能通过self myContent类的东西)然后返回它? How does it work? 它是如何工作的? I wonder also if there is a Design Pattern name for it. 我想知道是否有一个设计模式名称。

x at: 3 put: 123 sends at:put: selector to x . x at: 3 put: 123发送at:put: selector到x

There is well defined priority for selectors 选择器有明确的优先级

  1. unary ( 1 negated ) 一元( 1 negated
  2. binary ( 1 + 2 ) 二进制( 1 + 2
  3. keyword ( at:put: ) keyword( at:put:

The number of "arguments" for keywords selector is not relevant, it will always be interpreted as a single keyword selector, so detect:ifFound:ifNone: will be interpreted as a single selector which is sent to the object rather than three separate selectors. 关键字选择器的“参数”数量不相关,它将始终被解释为单个关键字选择器,因此detect:ifFound:ifNone:将被解释为发送到对象而不是三个单独选择器的单个选择器。

Now if you want to change the priority, or put one keyword selector inside another, you have to enclose it in parens. 现在,如果您想要更改优先级,或者将一个关键字选择器放在另一个中,则必须将其括在parens中。

So your x at: 3 put: 123 contains only one message: at:put: ; 所以你的x at: 3 put: 123只包含一条消息: at:put: ; if you actually want to send put: to the value present at position 3 , you need to put it in parens (x at: 3) put: 123 , which is also consistent with your observation about (x at: 3) + 567 . 如果你真的想把put:发送到位置3的值,你需要把它放在parens (x at: 3) put: 123 ,这也与你对(x at: 3) + 567观察结果一致。

Equivalent syntax from other languages would be x.atPositionInsert(3, 123) , and x.atPosition(3) + 567 . 来自其他语言的等效语法是x.atPositionInsert(3, 123)x.atPosition(3) + 567

There is no "cell" object. 没有“细胞”对象。 x at: 3 put: 123 puts the Integer 123 into the Collection x at index 3 . x at: 3 put: 123将Integer 123放入Collection x的索引3 x (the Collection, which may be of some more concrete type such as an Array) is responsible for storing the object and retrieving it when supplied with the appropriate index, but it doesn't create and store a different object. x (Collection,可能是一些更具体的类型,如Array)负责存储对象并在提供适当的索引时检索它,但它不会创建和存储不同的对象。 The internal workings of the Collection x are opaque from the point of view of the user of x - you don't know how x is storing the Integer in question - you only know that when you send the message at: with argument 3 to x you're going to get back what was stored there earlier - ie you'll get back 123 . 集合的内部运作x是从视图的用户的角度不透明x -你不知道怎么 x的存储问题的整数-你只知道,当你发送消息at:用争论3x你要回到之前存储的东西 - 也就是说你会回来123 So there's no "cell" object which has to interpret + - the Integer 123 is what's handling the + message. 所以没有“单元”对象必须解释+ - 整数123是处理+消息的。

It is very much possible to create classes which interpret arbitrary messages sent to them. 很有可能创建解释发送给它们的任意消息的类。 In versions of Smalltalk (eg Dolphin, Visual Smalltalk) I've worked with this is done by implementing the 'special' message doesNotUnderstand: . 在Smalltalk的版本中(例如Dolphin,Visual Smalltalk),我通过实现'特殊'消息来完成这项工作doesNotUnderstand: .

Hope this helps. 希望这可以帮助。

I might add to Peter's answer that the parenthesis is like parens in other languages, it forces the expression in parens to be evaluated first. 我可能会在Peter的回答中补充说,括号就像其他语言中的parens一样,它会强制首先评估parens中的表达式。 If there are multiple parens then evaluation goes left to right. 如果有多个parens,则评估从左到右。 So y := (x at: 3) + 567 For (x at: 3) is evaluated first which yields 123. 123 + 567 is evaluated second which yields 690 and y is set to 690. 因此,y:=(x at:3)+ 567首先评估(x at:3),得到123.第二次评估123 + 567,得到690,y设定为690。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM