Scala 中的 = 和 := 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7749530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between = and := in Scala?
提问by Jay Taylor
What is the difference between =and :=in Scala?
Scala=和:=Scala 中的区别是什么?
I have googled extensively for "scala colon-equals", but was unable to find anything definitive.
我在谷歌上广泛搜索了“scala Colon-equals”,但找不到任何明确的东西。
回答by Owen
=in scala is the actual assignment operator -- it does a handful of specific things that for the most part you don't have control over, such as
=在 scala 中是实际的赋值运算符——它执行一些在大多数情况下您无法控制的特定事情,例如
- Giving a
valorvara value when it's created - Changing the value of a
var - Changing the value of a field on a class
- Making a type alias
- Probably others
- 在创建时赋予一个
val或var一个值 - 改变 a 的值
var - 更改类上字段的值
- 制作类型别名
- 可能是其他人
:=is not a built-in operator -- anyone can overload it and define it to mean whatever they like. The reason people like to use :=is because it looks very assignmenty and is used as an assignment operator in other languages.
:=不是内置运算符——任何人都可以重载它并将其定义为任何他们喜欢的意思。人们喜欢使用的原因:=是因为它看起来很赋值,并且在其他语言中用作赋值运算符。
So, if you're trying to find out what :=means in the particular library you're using... my advice is look through the Scaladocs (if they exist) for a method named :=.
所以,如果你想找出:=你正在使用的特定库中的含义......我的建议是查看 Scaladocs(如果它们存在)中名为:=.
回答by tolitius
from Martin Odersky:
来自马丁·奥德斯基:
- Initially we had colon-equalsfor assignment—just as in Pascal, Modula, and Ada—and a single equals sign for equality. A lot of programming theorists would argue that that's the right way to do it. Assignment is not equality, and you should therefore use a different symbol for assignment. But then I tried it out with some people coming from Java. The reaction I got was, "Well, this looks like an interesting language. But why do you write colon-equals? What is it?" And I explained that its like that in Pascal. They said, "Now I understand, but I don't understand why you insist on doing that." Then I realized this is not something we wanted to insist on. We didn't want to say, "We have a better language because we write colon-equals instead of equals for assignment." It's a totally minor point, and people can get used to either approach. So we decided to not fight convention in these minor things, when there were other places where we did want to make a difference.
- 最初,我们使用冒号等号进行赋值——就像在 Pascal、Modula 和 Ada 中一样——以及一个等号来表示相等。许多编程理论家会争辩说这是正确的方法。赋值不是相等的,因此你应该使用不同的赋值符号。但后来我和一些来自 Java 的人一起尝试了它。我得到的反应是,“嗯,这看起来像是一种有趣的语言。但是你为什么要写冒号等于? 那是什么?”我解释说它就像在Pascal中一样。他们说,“现在我明白了,但我不明白你为什么坚持这样做。”然后我意识到这不是我们想要坚持的。我们不想说,“我们有更好的语言,因为我们在赋值时写冒号等于而不是等于。”这完全是次要的,人们可以习惯任何一种方法。所以我们决定不与传统作斗争这些小事,当我们确实想在其他地方有所作为时。
回答by Rex Kerr
=performs assignment. :=is not defined in the standard library or the language specification. It's a name that is free for other libraries or your code to use, if you wish.
=执行任务。 :=标准库或语言规范中未定义。如果您愿意,它是一个可供其他库或您的代码免费使用的名称。
回答by thomasrutter
Scala allows for operator overloading, where you can define the behaviour of an operator just like you could write a method.
Scala 允许运算符重载,您可以在其中定义运算符的行为,就像编写方法一样。
As in other languages, =is an assignment operator.
与其他语言一样,=是赋值运算符。
The is no standard operator I'm aware of called :=, but could define one with this name. If you see an operator like this, you should check up the documentation of whatever you're looking at, or search for where that operator is defined.
我知道 call 不是标准运算符:=,但可以用这个名称定义一个。如果您看到这样的运算符,您应该检查您正在查看的任何内容的文档,或者搜索该运算符的定义位置。
There is a lotyou can do with Scala operators. You can essentially make an operator out of virtually any characters you like.
有很多,你可以使用Scala运营商做。您基本上可以使用您喜欢的几乎任何字符来制作运算符。

