What is the difference between =
and :=
in Scala?
I have googled extensively for \"scala colon-equals\", but was unable to find anything de
=
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
val
or var
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 :=
.
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 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.