Smalltalk 中的消息传递真的都是这样吗?

发布于 2024-11-01 07:51:43 字数 479 浏览 1 评论 0原文

我是 Smalltalk 的新手,该语言中只有 6 个关键字(selfsupertrue, false, nil & thisContext),以及几乎所有内容都作为消息传递的纯粹程度,例如。使用 whileTrue 进行循环,使用 ifTrue 进行 if/else 等...这与我在其他语言中习惯的方式有很大不同。

然而,在某些情况下,我无法理解消息传递的真正意义,其中包括:

  • 赋值运算符 :=
  • 级联运算符 ;
  • 句点运算符 >.
  • 创建集合的方式#( ... )

这些不是消息传递,对吗?

I'm new to Smalltalk and I'm impressed with the fact that there are only just 6 keywords in the language (self, super, true, false, nil & thisContext), and how pure it is in having almost everything as message passing, eg. looping using whileTrue, if/else using ifTrue, etc ... which are way different from what I'm used to in other languages.

Yet, there are cases where I just cannot make sense of how message passing really fit in, these include:

  • the assignment operator :=
  • the cascading operator ;
  • the period operator .
  • the way to create a set #( ... )

These aren't message passing, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-11-08 07:51:43

正如您所发现的,仍然存在一些实际的 Smalltalk 语法。块构造、文字字符串/符号/注释、局部变量声明 (|...|) 和返回 (^) 是您没有提到的一些内容也是语法。

某些扩展(例如 #(...),通常创建一个 Array,而不是集合)当​​然可以以其他方式表达,例如 #(1 2 3 ) 相当于 Array with: 1 with: 2 with: 3;它们只是为了让代码更容易阅读和编写。

As you've discovered, there's still some actual Smalltalk syntax. Block construction, literal strings/symbols/comments, local variable declaration (|...|), and returning (^) are a few things you didn't mention which are also syntax.

Some extensions (e.g. #(...), which typically creates an Array, not a set) are certainly expressible otherwise, for example #(1 2 3) is equivalent to Array with: 1 with: 2 with: 3; they're just there to make the code easier to read and write.

半步萧音过轻尘 2024-11-08 07:51:43

一件事可能有助于澄清:selfsupertruefalsenil > & thisContext 是数据原语,而不是关键字。

它们是仅有的 6 个数据原语。这 6 个变量也称为伪变量。绝对所有其他事物都是类对象或其子类的实例。

Smalltalk 中预定义的关键字很少。它们可以以非常简洁的形式编写。

一个著名的例子是 Smalltalk Syntax on a Postcard (link)

 exampleWithNumber: x

    | y |
    true & false not & (nil isNil) ifFalse: [self halt].
    y := self size + super size.
    #($a #a "a" 1 1.0)
        do: [ :each |
            Transcript show: (each class name);
                       show: ' '].
    ^x < y

这是此方法的注释 - 这是大于方法本身:

“说明 Smalltalk 方法语法每个部分的方法
除了原语。它有一元、二进制和键盘消息,
声明参数和临时变量,访问全局变量
(但不是实例变量),使用文字(数组、字符、
符号、字符串、整数、浮点数),使用伪变量
true、false、nil、self 和 super,并且有序列、赋值、
返回和级联。它有零个参数和一个参数块。”

One thing that might help clarify : self, super, true, false, nil & thisContext are data primitives, rather than keywords.

They are the only 6 data primitives. These 6 are also known as pseudo-variables. Absolutely every other thing is an instance of Class Object or its subclasses.

There are very few pre-defined keywords in Smalltalk. They can be written in a very condensed form.

A famous example is Smalltalk Syntax on a Postcard (link)

 exampleWithNumber: x

    | y |
    true & false not & (nil isNil) ifFalse: [self halt].
    y := self size + super size.
    #($a #a "a" 1 1.0)
        do: [ :each |
            Transcript show: (each class name);
                       show: ' '].
    ^x < y

Here's the comment for this method - which is larger than the method itself:

"A method that illustrates every part of Smalltalk method syntax
except primitives. It has unary, binary, and keyboard messages,
declares arguments and temporaries, accesses a global variable
(but not an instance variable), uses literals (array, character,
symbol, string, integer, float), uses the pseudo variables
true, false, nil, self, and super, and has sequence, assignment,
return and cascade. It has both zero argument and one argument blocks."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文