Unique id for Scala object
object, scala
Solution
It is not possible to do this. You need to generate and keep track of IDs yourself.
`eq` compares references, and is similar to `==` in Java.
There is `System.identityHashCode` (which is the default implementation of `Any.hashCode`) but it is not guaranteed to be unique.
You may be able to implement `id` using the JNI, although it might not be trivial due to the possibility of a compacting GC being in place. I don't know much about the JNI, though.
Even then, I don't see `id` as a useful function and I don't see any use for it. If you need a reference to an object, store a reference. If you need a weak reference, use a `java.lang.ref.WeakReference[T]`.
Problem
In Python, id(x) gives the unique id of object x. What's the equivalence in Scala? ``` >>> id(True) 1166096 >>> id(False) 1166108 >>> x = id([1,2,3]) >>> id(x) 2058589988 >>> y = id([1,2,3]) >>> id(y) 2058589976 ``` I could use x.hashCode for the id, but it will return the same value when the contents are the same. I'd like to know what makes `a eq b == false` in the following code. What values are compared in `a eq b`? ``` scala> val a = ArrayBuffer(1,2,3) a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) scala> val b = ArrayBuffer(1,2,3) b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) scala> a.hashCode res44: Int = 387518613 scala> b.hashCode res45: Int = 387518613 scala> a eq b res39: Boolean = false ```