Is it possible, in Scala, to define a function that would receive any other function as a parameter?
It should be something like the following:
object Module extends SecureModule{ val bc = new MyBC() def method(parameter: Type) = { exec(bc.method(parameter)) } def method2(parameter1: Type1, parameter2: Type2) = { exec(bc.method2(parameter1,parameter2)) } } trait SecureModule { def exec(f: ANY_PARAMETER => ANY_RESULT) = { //some extra processing f } } is it possible? If so, how could I achieve this?
Thank you in advance.
ANY_PARAMETER => ANY_RESULTis slightly, but importantly, misleading in that it implies that there is always one parameter, whereas you apparently want this to work with a function of any arity. Similarly, the invocationf(which should presumably be f()) implies that there are no arguments. Arity is the real problem here; for a given arity, abstracting over the types is simple.