Czar
12/27/2018, 12:49 PM@Service
public class CommandService {
public <C extends Command, R: Any> R execute(C command) {/*...*/}
}
in Kotlin I want to somehow be able to write SomeCommand(/* properties */).execute()
Currently to achieve that in some of the code following approach is used: adding private fun <R : Any> Command.execute(): R = commandService.execute(this)
to all of the client classes as well as an autowired val commandService: CommandService
.
I need to change that as this happens more often it's too much repetition.
I've came up with the following, but it looks a bit hacky to me.
fun <T : Any> Command.execute(): T = STATIC_COMMAND_SERVICE.execute(this)
private lateinit var STATIC_COMMAND_SERVICE: CommandService
@Configuration
class CommandServiceKotlinInitializer(commandService: CommandService) {
init {
STATIC_COMMAND_SERVICE = commandService
}
}