Scala 中 = 和 := 有什么区别?

发布于 2024-12-09 17:32:40 字数 107 浏览 0 评论 0原文

Scala 中 =:= 有什么区别?

我在谷歌上广泛搜索了“scala colon-equals”,但无法找到任何明确的信息。

What is the difference between = and := in Scala?

I have googled extensively for "scala colon-equals", but was unable to find anything definitive.

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

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

发布评论

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

评论(4

◇流星雨 2024-12-16 17:32:40

scala 中的 = 是实际的赋值运算符——它会执行一些您无法控制的特定操作,例如

  • 给出一个 valvar 创建时的值
  • 更改 var 的值
  • 更改类上字段的值
  • 创建类型别名
  • 可能还有其他

:=不是内置运算符——任何人都可以重载它并将其定义为任何他们想要的含义 喜欢。人们喜欢使用 := 的原因是因为它看起来非常赋值,并且在其他语言中被用作赋值运算符。

因此,如果您想了解 := 在您正在使用的特定库中的含义...我的建议是查看 Scaladocs(如果存在)中名为 :=

= 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

  • Giving a val or var a 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

:= 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 :=.

踏雪无痕 2024-12-16 17:32:40

来自 Martin Odersky:

  • 最初,我们使用冒号等于来进行赋值(就像 Pascal、Modula 和 Ada 中一样)以及一个表示相等的等号。许多编程理论家认为这是正确的方法。赋值并不相等,因此您应该使用不同的符号进行赋值。但后来我和一些来自 Java 的人尝试了一下。我得到的反应是,“嗯,这看起来是一种有趣的语言。但是你为什么要写冒号等于?它是什么?”我解释说这就像 Pascal 中的那样。他们说:“现在我明白了,但我不明白你为什么坚持这样做。”然后我意识到这不是我们想要坚持的事情。我们不想说,“我们有更好的语言,因为我们在赋值时写的是冒号等于而不是等于。”这完全是一个小问题,人们可以习惯这两种方法。因此,当我们确实想在其他地方做出改变时,我们决定不在这些小事情上违背惯例。

来自 Scala 设计的目标

from Martin Odersky:

  • Initially we had colon-equals for 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.

from The Goals of Scala's Design

晨敛清荷 2024-12-16 17:32:40

= 执行赋值。 := 未在标准库或语言规范中定义。如果您愿意,这个名称可以免费供其他库或您的代码使用。

= 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.

丢了幸福的猪 2024-12-16 17:32:40

Scala 允许运算符重载,您可以像编写方法一样定义运算符的行为。

与其他语言一样,= 是赋值运算符。

据我所知,:= 不是一个标准运算符,但可以用这个名称定义一个。如果您看到这样的运算符,您应该检查您正在查看的任何内容的文档,或者搜索定义该运算符的位置。

使用 Scala 可以做很多运营商。您基本上可以用几乎任何您喜欢的角色来制作操作员。

Scala allows for operator overloading, where you can define the behaviour of an operator just like you could write a method.

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.

There is a lot you can do with Scala operators. You can essentially make an operator out of virtually any characters you like.

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