Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala reference equality

Tags:

scala

How do you test reference equality in Scala?

val x = new Obj val y = x x.referenceEquals(y) // evaluates to true 
like image 946
Landon Kuhn Avatar asked Jul 26 '11 19:07

Landon Kuhn


People also ask

Does == check for reference?

== operator is used to check whether two variables reference objects with the same value.

What does == mean in Scala?

But in Scala, == is testing for value equality. Let's understand with example. Example : Scala.

What is the difference between .equals and == in 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.

Which is used to check the reference equality?

Use the ReferenceEquals method to determine whether two references refer to the same object. The concept of reference equality applies only to reference types. Value type objects cannot have reference equality because when an instance of a value type is assigned to a variable, a copy of the value is made.


1 Answers

The function you are looking for is eq, which is a member of AnyRef:

val x = new Obj val y = x x eq y // evaluates to true x ne y // evaluates to false 
like image 124
JAiro Avatar answered Sep 19 '22 21:09

JAiro