Scala reference equality

2020-06-06 08:31发布

问题:

How do you test reference equality in Scala?

val x = new Obj
val y = x
x.referenceEquals(y) // evaluates to true

回答1:

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


标签: scala