Is it possible to reduce such a structure?

class Navigator {
 companion object {
 private fun add(command: ICommand) {...}
 private fun replace(fragment: Fragment) {...}

 fun showMain() = add(object : ICommand {
 override fun execute() {
replace(MainFragment.newInstance())
}
 })

because in Java it looks like this:
public static void showMain() {
 add(() -> replace(MainFragment.newInstance()));
 }
March 19th 20 at 08:30
2 answers
March 19th 20 at 08:32
class Navigator {
 companion object {
 private inline fun add(command: () -> Unit) {}
 private fun replace(fragment: Fragment) {}
 fun showMain() = add { replace(MainFragment.newInstance()) }
}
}


so, for example
perhaps inline is not quite what you need (and no syntax checking takes place)
full view function like this
private fun add(command: ICommand) {
App.instance.commandExecutor.add(command)
 }

the point is that we create an object which is able to perform the action and put it in by teams
ie option in add needs to be an object that implements ICommand,
the replace function is one of the possible actions within ICommand, there are other - rashawn62 commented on March 19th 20 at 08:35
@rashawn62, So rather than lambda does not fit the team? - alvis_T commented on March 19th 20 at 08:38
March 19th 20 at 08:34
it seems to be understood
private var add = { command: ICommand ->App.instance.commandExecutor.add(command) }
private var replace = { fragment: Fragment> ..... }

fun showMain() = add(object : ICommand {
 override fun execute() = replace(MainFragment.newInstance())
})

from an anonymous object as I understand it does not get rid of?

Find more questions by tags Kotlin