Smalltalk 中的运算符可以过载吗?
Smalltalk 中是否可以重载运算符?
我正在寻找教程/示例。
谢谢。
Is it possible to overload operators in Smalltalk?
I am looking for tutorials/examples.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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 ofInteger>>+
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...大多数情况下,其他语言中的运算符都在 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.
除了赋值之外,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.
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 asIn 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
, ClassSmallInt
, ClassFraction
and ClassPoint
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 toaFraction
"aSum := aFraction + aSmallInt
Evaluates to:64/7
"send the
+ aFraction
message toaSmallInt
"aSum := aSmallInt + aFraction
Evaluates to:64/7
aSum := aFloat + aFraction
These evaluate to:aSum := aFraction + aFloat
6.284357142857143
aSum := aFloat + aSmallInt
These evaluate to:aSum := aSmallInt + aFloat
9.1415
aSum := aPoint + aSmallInt
These evaluate to:aSum := aSmallInt + aPoint
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.
Smalltalk 没有运算符,但您可以通过方法定义/覆盖来实现类似的事情:
Smalltalk doesn't have operators but you can achieve a similar thing via method definition/overriding: