In the example below, the variable fn2_class
represents a function that is to be loaded from a lua script, hence the var
definition is necessary; It is not known in compile time what the actual function will be. The variable fn1_class
represents the statically bound version of the same idea. The only difference between fn1_class
and fn2_class
is thus that the former is defined as a value and the latter as a variable.
The method getFunction
represents how the lua function is loaded. The real code includes calls to the luaj library to generate the function.
Rephrasing the question with this context in mind: How to define a variable fn3_class
that is dynamically bound and has the same call syntax as the value fn1_class
in the example?
This question touches scala-2.10 or later. This kind of code was compiling and executing in scala-2.9.
Example code illustrating the problem:
object Class {
// static binding
val fn1_class: (String*) => String = getFunction()
// dynamic binding
var fn2_class: (String*) => String = null
def getFunction(): (String*) => String = {
val fn_dyn : (String*) => String = x => { x.head }
fn_dyn
}
def main(args: Array[String]): Unit = {
fn2_class = getFunction()
println( fn1_class() )
println( fn2_class() )
}
}
The above code generates, when complied, the following error message (scala-2.10).
error: not enough arguments for method apply: (v1: String)String in trait Function1.
Unspecified value parameter v1.
println( fn2_class() )
^