How can you compare two Scala function values for equality. The use case is that I have a list of functions where the list can contain duplicates and I only want to execute each function once.
If I have:
scala> object A {
| def a {}
| }
defined module A
scala> val f1 = A.a _
f1: () => Unit = <function0>
scala> val f2 = A.a _
f2: () => Unit = <function0>
If I try to compare the function with either == or eq, I will get false in both cases:
scala> f1 == f2
res0: Boolean = false
scala> f1 eq f2
res1: Boolean = false
Short answer: It’s not possible.
Longer answer: You could have some kind of function factory that ensures that “identical” functions are acutally the same object. Depending on the architecture of your application, that might not be feasible though.