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.Joshy Josh
12/04/2020, 11:20 PMJoshy Josh
12/04/2020, 11:22 PMinterface BaseRobot {
fun <T> screen(init: T.() -> Unit): T = init as T
}
Joshy Josh
12/04/2020, 11:22 PMShawn
12/05/2020, 12:36 AMJoshy Josh
12/05/2020, 12:38 AMscreen<class> { function() }
Joshy Josh
12/05/2020, 12:39 AMShawn
12/05/2020, 12:39 AMrun
, with
, or apply
Joshy Josh
12/05/2020, 12:40 AMShawn
12/05/2020, 12:40 AMShawn
12/05/2020, 12:44 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 AMJoshy Josh
12/05/2020, 12:45 AMfun screen(bot: Bot.()-> Unit): Bot = Bot().apply(bot())
Joshy Josh
12/08/2020, 5:41 AMJoshy Josh
12/08/2020, 5:43 AMInline fun <reified T: Any> screen(bot: T.() -> Unit): T? = T::class.java.newInstance().apply { bot() }
Joshy Josh
12/08/2020, 5:45 AMscreen<ScreenObject> { functionsFromScreenObjectClass }