vendredi 17 janvier 2020

Is it possible to delegate methods implementations

Given a function which creates lambdas. Is it possible to delegate the implementation of a declared function to a returned lambda (analogue to by-delegated properties). If by would exist for functions it could look like this:

private fun printFunCreator(): () -> Unit { return { println("Hello world")} }
fun printHelloWorld() by printFunCreator()

What I am not looking for:

I do not want to create an extra property of the lambda to delegate the call like this:

private fun printFunCreator(): () -> Unit { return { println("Hello world")} }
private val printFun = printFunCreator()
fun printHelloWorld() = printFun.invoke()

And I do not want to create a new lambda instance for each execution like this:

private fun printFunCreator(): () -> Unit { return { println("Hello world")} }
fun printHelloWorld() = printFunCreator().invoke()




Aucun commentaire:

Enregistrer un commentaire