If you want your wrapper method to execute the wrapped method within itself, you must change the "by name" parameter. This uses the syntax => ResultType .
def wrapper(f: => Any): Any = { println("Executing now") val res = f println("Execution finished") res }
Now you can do it,
wrapper { println("2") }
and he will print
Executing now 2 Execution finished
If you want to use the return type of a wrapped function, you can make your general method:
def wrapper[T](f: => T): T = { println("Executing now") val res: T = f println("Execution finished") res }
Ben lings
source share