什么使Scala的运算符重载“好”,但是C ++的“坏”? [英] What makes Scala's operator overloading "good", but C++'s "bad"?

查看:179
本文介绍了什么使Scala的运算符重载“好”,但是C ++的“坏”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作符重载在C ++中被许多人认为是坏事(tm),并且错误不会在较新的语言中重复。当然,这是在设计Java时特别删除的一个特性。

Operator overloading in C++ is considered by many to be A Bad Thing(tm), and a mistake not to be repeated in newer languages. Certainly, it was one feature specifically dropped when designing Java.

现在我开始在Scala上阅读,我发现它有什么看起来非常像运算符重载(虽然在技术上它没有运算符重载,因为它没有运算符,只有函数)。但是,它似乎与C ++中的运算符重载似乎没有质的不同,在这里我记得运算符被定义为特殊函数。

Now that I've started reading up on Scala, I find that it has what looks very much like operator overloading (although technically it doesn't have operator overloading because it doesn't have operators, only functions). However, it wouldn't seem to be qualitatively different to the operator overloading in C++, where as I recall operators are defined as special functions.

所以我的问题是什么在C ++中定义+的想法比C ++中更好的想法。

So my question is what makes the idea of defining "+" in Scala a better idea than it was in C++?

推荐答案

C ++继承了C 。我的意思是6 + 4中的+很特别。例如,你不能获得指向该+函数的指针。

C++ inherits true blue operators from C. By that I mean that the "+" in 6 + 4 is very special. You can't, for instance, get a pointer to that + function.

另一方面,Scala没有这样的操作符。它只是非常灵活地定义方法名称加上一些非字符号的优先级。所以技术上Scala没有运算符重载。

Scala on the other hand doesn't have operators in that way. It just has great flexibility in defining method names plus a bit of built in precedence for non-word symbols. So technically Scala doesn't have operator overloading.

无论你想调用什么,操作符重载本身并不坏,即使在C ++。问题是坏程序员滥用它。但坦率地说,我认为,取走程序员滥用运算符重载的能力不会减少解决程序员可以滥用的所有事情。真正的答案是指导。 http://james-iry.blogspot.com/2009/03/operator- overloading-ad-absurdum.html

Whatever you want to call it, operator overloading isn't inherently bad, even in C++. The problem is when bad programmers abuse it. But frankly, I'm of the opinion that taking away programmers ability to abuse operator overloading doesn't put a drop in the bucket of fixing all the things that programmers can abuse. The real answer is mentoring. http://james-iry.blogspot.com/2009/03/operator-overloading-ad-absurdum.html

无论如何,C ++的运算符重载和Scala的灵活方法命名之间有区别,IMHO,scala

None-the-less, there are differences between C++'s operator overloading and Scala's flexible method naming which, IMHO, make Scala both less abusable and more abusable.

在C ++中,获取in-fix符号的唯一方法是使用运算符。否则必须使用object.message(argument)或pointer-> messsage(argument)或function(argument1,argument2)。所以如果你想要某种DSLish风格的代码,那么就有压力使用运算符。

In C++ the only way to get in-fix notation is using operators. Otherwise you must use object.message(argument) or pointer->messsage(argument) or function(argument1, argument2). So if you want a certain DSLish style to your code then there's pressure to use operators.

在Scala中,你可以得到任何消息发送的中缀符号。 对象消息参数是完全确定的,这意味着你不需要使用非字符号只是为了获得中缀符号。

In Scala you can get infix notation with any message send. "object message argument" is perfectly ok, which means you don't need to use non-word symbols just to get infix notation.

C ++操作符重载限于本质C运算符。结合限制,只有运算符可以使用中缀,压力,人们试图映射到一个相对较少的符号,如+和>>的大量不相关的概念映射

C++ operator overloading is limited to essentially the C operators. Combined with the limitation that only operators may be used infix that puts pressure on people to try to map a wide range of unrelated concepts onto a relatively few symbols like "+" and ">>"

Scala允许大量的有效非字符号作为方法名。例如,我有一个嵌入的Prolog-ish DSL,你可以写

Scala allows a huge range of valid non-word symbols as method names. For instance, I've got an embedded Prolog-ish DSL where you can write

female('jane)!         // jane is female
parent('jane,'john)!   // jane is john's parent
parent('jane, 'wendy)! // jane is wendy's parent

mother('Mother, 'Child) :- parent('Mother, 'Child) & female('Mother) //'// a mother of a child is the child's parent and is female

mother('X, 'john)?  // find john's mother
mother('jane, 'X)?  // find's all of jane's children

: - ,!,?和&符号被定义为普通方法。在C ++中&将有效,所以尝试将这个DSL映射到C ++将需要一些已经引起非常不同的概念的符号。

The :-, !, ?, and & symbols are defined as ordinary methods. In C++ only & would be valid so an attempt to map this DSL into C++ would require some symbols that already evoke very different concepts.

当然,这也打开了Scala到另一种滥用。在Scala中,你可以命名一个方法$!& ^%如果你想。

Of course, this also opens up Scala to another kind of abuse. In Scala you can name a method $!&^% if you want to.

对于像Scala这样的其他语言,在使用非词函数和方法名称时是灵活的,参见Smalltalk,像Scala一样,每个运算符方法和Haskell,它允许程序员定义灵活命名函数的优先级和固定性。

For other languages that, like Scala, are flexible in the use of non-word function and method names see Smalltalk where, like Scala, every "operator" is just another method and Haskell which allows the programmer to define precedence and fixity of flexibly named functions.

这篇关于什么使Scala的运算符重载“好”,但是C ++的“坏”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆