Joshy Josh
12/04/2020, 11:19 PMfunctionName<ClassName> { …className functions}
? I know it can be done specifically, but I was wondering if there was a way to create a generic approach. in Kotlin.interface BaseRobot {
fun <T> screen(init: T.() -> Unit): T = init as T
}
Shawn
12/05/2020, 12:36 AMJoshy Josh
12/05/2020, 12:38 AMscreen<class> { function() }
Shawn
12/05/2020, 12:39 AMrun
, with
, or apply
Joshy Josh
12/05/2020, 12:40 AMShawn
12/05/2020, 12:40 AMT.() -> R
is the correct approach, but casting the lambda to T
will never work (at least the way you’ve written it in your example)Joshy Josh
12/05/2020, 12:44 AMfun screen(bot: Bot.()-> Unit): Bot = Bot().apply(bot())
Inline fun <reified T: Any> screen(bot: T.() -> Unit): T? = T::class.java.newInstance().apply { bot() }
screen<ScreenObject> { functionsFromScreenObjectClass }