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

后端 未结 4 1037
一个人的身影
一个人的身影 2020-12-24 00:44

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

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

4条回答
  •  死守一世寂寞
    2020-12-24 01:15

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

提交回复
热议问题