本文介绍了":="和Smalltalk中的二进制消息优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Dolphin Smalltalk X6随附的教程来学习Smalltalk.

I am attempting to learn Smalltalk through the tutorials included with Dolphin Smalltalk X6.

我的问题涉及对带有多条消息的表达式的求值.

My question deals with the evaluation of expressions with multiple messages.

我的理解是,首先评估简单消息,然后评估二进制消息,最后评估关键字消息(括号中的代码除外).但是,在以下示例中,我很难将这种理解应用于第二行(可在海豚Smalltalk教程).

My understanding is that simple messages are evaluated first, then binary messages, and finally keyword messages (with the exception of code in parenthesis). However, I am having trouble applying this understanding to the second line in the following example (found in the Dolphin Smalltalk tutorial).

    playground := Playground new.
    teresa := playground add: Triangle new.       "Misunderstood code"
    teresa class.                                 "Evaluates to 'Triangle'"

如果我的理解正确,那么第二行将被评估:

If my understanding were correct, the 2nd line would be evaluated thusly:

    1. Simple message 'new' sent to Triangle, triangle object as response
    2. Binary message ':=' with parameter 'playground' sent to 'teresa'.
    3. Keyword message 'add:' with parameter 'triangle object' sent to 'teresa'.
    4. teresa class. "evaluates to 'Playground'".

我的误解是特蕾莎修女"是指匿名的Triangle对象,而不是"playground"所指的Playground对象.

My misunderstanding is in how 'teresa' comes to refer to the anonymous Triangle object and not the Playground object referred to by 'playground'.

我查找了Smalltalk评估的第二种解释,以提及:=或add:是没有成功的特殊情况,而我能想到的唯一其他解释是一个基本的误解.

I have looked up a second explanation of Smalltalk evaluation for mentions of := or add: being special cases with no success, and the only other explanation I can think of is a fundamental misunderstanding.

有人帮助我吗?

推荐答案

虽然:=看起来像二进制消息,因为它使用了中缀字符,但不是.这是语言语法的一部分,与parens和句点一样(例如).

While := looks like a binary message because it uses infix characters... it is not. It's a part of the language syntax in the same way that parens and periods are (for example).

以:=作为赋值运算符(有时称为"gets").它不是任何优先级的消息.发送任何消息后都会对它进行评估.它的左值必须是直接变量引用(而不是消息发送的结果).

Think of := as the assignment operator (sometimes spoken as "gets"). It is NOT a message of any precedence. It is evaluated AFTER any message sends. It's lvalue MUST be a direct variable reference (not the results of a message send).

这篇关于":="和Smalltalk中的二进制消息优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:22