This helpful StackOverflow answer states that it's not possible to compare two functions.
For the following, I get `false, as expected:
def
scala> def add1Test: Int => Int = { println("3"); _ + 1 }
add1Test: Int => Int
scala> add1Test == add1Test
3
3
res4: Boolean = false
scala> :t add1Test(5)
Int
Function1
scala> def foo: Function1[Int, Int] = _ + 1
foo: Int => Int
scala> :type foo
Int => Int
scala> foo == foo
res6: Boolean = false
val
However, for a val
, I get true.
scala> val valAdd1: Int => Int = { println("val"); _ + 1}
val
valAdd1: Int => Int = <function1>
scala> valAdd1 == valAdd1
res3: Boolean = true
scala> :t valAdd1
Int => Int
Why does comparing def
and Function
result in false, but true for a val
?
Also, why is false, rather than other behavior (ex: Exception thrown), returned when comparing a def
and Function
?