Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 426
Jay Taylor Avatar asked Oct 13 '11 04:10

Jay Taylor


People also ask

What does := mean in Scala?

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

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

In general: :: - adds an element at the beginning of a list and returns a list with the added element. ::: - concatenates two lists and returns the concatenated list.

What is the difference between equals () and ==? Scala?

== is a final method, and calls . equals , which is not final. This is radically different than Java, where == is an operator rather than a method and strictly compares reference equality for objects.

Why === is used in Scala?

The triple equals operator === is normally the Scala type-safe equals operator, analogous to the one in Javascript. Spark overrides this with a method in Column to create a new Column object that compares the Column to the left with the object on the right, returning a boolean.


1 Answers

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

like image 110
Owen Avatar answered Sep 20 '22 12:09

Owen