Scala 中 = 和 := 有什么区别?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
scala 中的
=
是实际的赋值运算符——它会执行一些您无法控制的特定操作,例如val
或var
创建时的值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 asval
orvar
a value when it's createdvar
:=
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:=
.来自 Martin Odersky:
来自 Scala 设计的目标
from Martin Odersky:
from The Goals of Scala's Design
=
执行赋值。:=
未在标准库或语言规范中定义。如果您愿意,这个名称可以免费供其他库或您的代码使用。=
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.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.