Scala 中的 = 和 := 有什么区别? [英] What is the difference between = and := in Scala?

查看:75
本文介绍了Scala 中的 = 和 := 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我在谷歌上广泛搜索了scala Colon-equals",但找不到任何明确的内容.

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

推荐答案

= 在 scala 中是实际的赋值操作符——它做了一些特定的事情,在大多数情况下你不会有控制权,例如

= 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

  • 在创建时给 valvar 一个值
  • 更改 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.

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

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

这篇关于Scala 中的 = 和 := 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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