Smalltalk 中的运算符可以过载吗?

发布于 2024-11-17 23:20:36 字数 60 浏览 1 评论 0原文

Smalltalk 中是否可以重载运算符?

我正在寻找教程/示例。

谢谢。

Is it possible to overload operators in Smalltalk?

I am looking for tutorials/examples.

Thanks.

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

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

发布评论

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

评论(5

彩扇题诗 2024-11-24 23:20:36

Smalltalk 中不可能进行方法重载。相反,方法重写和称为双重调度的技术的组合用于实现与其他语言中的运算符重载相同的行为。

您可以在数学运算符 +,*,/,-(Smalltalk 中的二进制消息)中找到示例实现。其想法如下:Integer>>+ 的实现向其参数发送一条消息#addWithInteger:#addWithInteger: 的实现是在每个 Magnitude 子类上实现的,例如专门用于 Int+Int、Float+Int 等的加法...

Method overloading is not possible in Smalltalk. Instead, a combination of method overriding and a technique called double dispatch is used to implement the same behavior as operator overloading in other languages.

You can find an example implementation in the mathematical operators +,*,/,- (which are binary messages in Smalltalk). Here is the idea: the implementation of Integer>>+ sends a message #addWithInteger: to its argument. The implementation of #addWithInteger: is implemented on each Magnitude subclass, such as to specialize addition of Int+Int, Float+Int, etc...

鯉魚旗 2024-11-24 23:20:36

大多数情况下,其他语言中的运算符都在 Smalltalk 一元或二进制消息中,例如 +、*、/、... 等。类可以自由地响应这些消息,因为它们看起来合适,所以是的,您可以重新定义行为+,你也可以让一些非数类的实例理解并响应它。

例如看一下 Point 类中 + 的实现。

需要注意的一件事是,:= 和 ^ 不是消息,因此它们不能以上述方式重新定义。

顺便说一句,对于学习 Smalltalk,最好的示例和代码资源之一是 Smalltalk 图像。因此,我建议您启动 Smalltalk,并学习如何浏览其中包含的大量示例。

For most part, things that are operators in other languages are in Smalltalk unary or binary messages like +, *, /, ... etc. Classes are free to respond to those messages as they seem fit, so yes, you can redefine behavior of +, and you can also make instances of some non number classes understand and respond to it.

For instance look at the implementation of + in Point class.

One thing to note, := and ^ are not messages, so they can not redefined in a way described above.

Btw, for learning Smalltalk, one of the greatest resources of examples and code is the Smalltalk image. So I recommend that you fire up Smalltalk, and learn your way to browse through vast amount of examples that it contains.

行雁书 2024-11-24 23:20:36

除了赋值之外,smalltalk 中没有运算符。一切都作为方法在类中实现。因此,如果您想更改 = 或 +/- 方法的行为,只需查看它们的实现者即可。
或者,如果您想创建类的实例来理解这些消息,只需实现它们即可。

There's no operators in smalltalk, except assignment. Everything is implemented in classes as methods. So if you want to change behaviour of = or + / - methods, just look at their implementors.
Or if you want to make instances of your class to understand those messages, just implement them.

北恋 2024-11-24 23:20:36

operator-overloading 标签在 Stack Overflow 上定义为

编程语言的一项功能,允许根据所涉及的操作数的类型自定义运算符的实现。有些语言允许定义新的运算符,而其他语言只允许重新定义现有的运算符。

在 Smalltalk 中
所有类型都定义为对象的类 *
所有运算符都是方法 *
所有方法均由带有方法名称的消息接收者执行
所有方法都可以被重写,

因此任何开发人员都可以重写对任何操作数进行操作的任何运算符。

以下是一些示例:
Float 类、SmallInt 类、Fraction 类和 Point 类的对象都可以响应 +消息。它们也可以相互交互。
aFloat := 3.1415 。
aSmallInt := '6' 。
a点:= 3@3 。
aFraction := 22/7 .

"将 + aSmallInt 消息发送至 aFraction"
aSum := aFraction + aSmallInt 计算结果为:64/7

“将 + aFraction 消息发送到 aSmallInt”< br>
aSum := aSmallInt + aFraction 计算结果为:64/7

aSum := aFloat + aFraction
aSum := aFraction + aFloat
这些计算结果为:6.284357142857143

aSum := aFloat + aSmallInt
aSum := aSmallInt + aFloat
这些计算结果为:9.1415

aSum := aPoint + aSmallInt
aSum := aSmallInt + aPoint
这些计算结果为:9@9

实际上,我们在这里显示了 + 运算符的 8 种不同实现,每种都自定义为处理涉及的操作数的类型。

注意事项:
* 对象不是强类型的。任何一种类型的变量都可以更改为任何其他类型,并且系统不会引发异常。一个对象可以从 SmallInt 类的对象开始,然后更改为 ByteString 或 Dictionary,系统不会发出丝毫警告。直到发送一条它不理解的消息为止。

有 6 个基元不是对象或对象类:
true、false、nil 等。

有两个运算符实际上是命名方法的语法糖。

The operator-overloading tag is defined on Stack Overflow as

a feature of a programming language that allows custom implementations for operators depending on the types of the operands involved. Some languages allow new operators to be defined while others only allow redefinition of existing ones.

In Smalltalk
All types are defined as classes of object *
All operators are methods *
All methods are executed by the receiver of the message with the method's name
All methods can be over-ridden

So any operator, operating on any operand, can be over-ridden by any developer.

Here are some examples:
Objects of Class Float, Class SmallInt, Class Fraction and Class Point can all respond to a + message. They can interroperate with one another, too.
aFloat := 3.1415 .
aSmallInt := '6' .
aPoint := 3@3 .
aFraction := 22/7 .

"send the + aSmallInt message to aFraction"
aSum := aFraction + aSmallInt Evaluates to: 64/7

"send the + aFraction message to aSmallInt"
aSum := aSmallInt + aFraction Evaluates to: 64/7

aSum := aFloat + aFraction
aSum := aFraction + aFloat
These evaluate to: 6.284357142857143

aSum := aFloat + aSmallInt
aSum := aSmallInt + aFloat
These evaluate to: 9.1415

aSum := aPoint + aSmallInt
aSum := aSmallInt + aPoint
These evaluate to: 9@9

In effect, we have 8 different implementations of the + operator on display here, each customised to deal with the types of the operands involved.

The caveats:
* Objects are not strongly typed. Any variable of one type can be changed to any other type, and the system will not raise an exception. An object can start as an object of Class SmallInt and then be changed to a ByteString or a Dictionary, and the system will not raise the slightest warning. Until it is sent a message that it does not understand.

There are 6 primitives that are not an object or Class of object:
true, false, nil, etc.

There are two operators which are, in effect, syntactic sugar for named methods.

2024-11-24 23:20:36

Smalltalk 没有运算符,但您可以通过方法定义/覆盖来实现类似的事情:

Object subclass: Foo [ 
  + anObject [
    "define new behavior for + here"

  ]
]

Smalltalk doesn't have operators but you can achieve a similar thing via method definition/overriding:

Object subclass: Foo [ 
  + anObject [
    "define new behavior for + here"

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