Is it possible to overload operators in Smalltalk?
I am looking for tutorials/examples.
感谢。
Is it possible to overload operators in Smalltalk?
I am looking for tutorials/examples.
感谢。
小塔克不可能采用超负荷方法。 相反,使用一种压倒一切的办法和一种称为杜布尔发送<>/a>)与操作者用其他语文超载同一行为。
可在数学操作者<代码>+,*,/,-上找到一个实例(在小塔克是双向电文)。 这里的想法是:实施<代码>。 Integer>>+向论点发出#WaddInteger:
。 执行<条码>#addWithInteger:的每个彩虹子类别,例如专门增加Int+Int、Float+Int等......
多数情况下,其他语文的操作者使用的是小塔里克非固定或双双双电信息,如+、*、/......等。 班级可以自由回应这些似乎适合的信息,因此,你可以重新界定“+”行为,而且你还可以让一些非班级了解和回应。
例如,研究点级+的实施情况。
值得注意的是:=和 ^不是信息,因此无法以上述方式重新定义。
为学习小塔克,实例和密码的最大资源之一是小塔克形象。 因此,我建议你向小塔克开枪,并学习你通过其中的大量实例浏览。
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.
<代码>自动载荷-超载标签在Stack Overflow上下定义
一种方案拟定语言的特征是,根据所涉操作类型,允许操作者采用习惯。 某些语言允许对新的操作者进行定义,而其他语言只允许重新界定现有操作者。
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.
目前有两家经营人,实际上都是用有名的方法种植合成糖。
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"
]
]
I need to overload the stream extraction operator. I need to do this by allowing a user to input a string of characters at a prompt, say "iamastring", and then the operator would extract each ...
I m working on a programming language that uses C++ as it s target language for now. I m hitting an exceptionally strange backtrace. #1 0x08048d09 in factorial (n=0x8052160) at ir.cpp:35 35 ...
I m a little confused as to why I ve been told to return const foo from a binary operator in c++ instead of just foo. I ve been reading Bruce Eckel s "Thinking in C++", and in the chapter on operator ...
I d like to create my own class extending array of ints. Is that possible? What I need is array of ints that can be added by "+" operator to another array (each element added to each), and compared by ...
C++ continues to surprise me. Today i found out about the ->* operator. It is overloadable but i have no idea how to invoke it. I manage to overload it in my class but i have no clue how to call it. ...
template <typename T> class v3 { private: T _a[3]; public: T & operator [] (unsigned int i) { return _a[i]; } const T & operator [] (unsigned int i) const { return _a[i]; } ...
I have a class in which I m trying to overload the << operator. For some reason, it is not being overloaded. Here is my .h file: friend std::ostream& operator<<(std::ostream&, ...
I can t seem to find the way to overload the [] operator in javascript. Anyone out there know? I was thinking on the lines of ... MyClass.operator.lookup(index) { return myArray[index]; } or am ...