With the following code:
object Foo {
val arr: Int => Int = Array(1,2,3)
}
We can call Foo.arr(1), then get 2. However, arr in object Foo should be a Function. The arr is an instance of Class WrappedArray$ofInt, which is obviously not a Function.
I guess maybe the reason is WrappedArray$ofIntimplements apply(i: Int), because the call Array(1,2,3)(1) will be translated into Array(1,2,3).apply(1). But the following code would be error:
class Bar {
def apply(i: Int): Int = {
Array(1,2,3)(i)
}
}
object Foo {
val arr: Int => Int = new Bar() //Type mismatch:
// Required: Int => Int
// Found: Bar
}
Now, why val arr: Int => Int = Array(1,2,3) is allowed in scala?