D
D
Dmtm2019-03-25 16:46:44
Kotlin
Dmtm, 2019-03-25 16:46:44

Is it possible to shorten 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()));
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tiberal, 2019-03-26
@Tiberal

class Navigator {
    companion object {
        private inline fun add(command: () -> Unit) {}
        private fun replace(fragment: Fragment) {}
        fun showMain() = add { replace(MainFragment.newInstance()) }
    }
}

for example

D
Dmtm, 2019-03-26
@Dmtm

seems to have figured it out

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())
})

As I understand it, there is no way to get rid of an anonymous object?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question