Shorthand for lambda that's just a function call

Consider this code:

root.findViewById<Button>(R.id.button1).setOnClickListener {
	AlertDialog.Builder(requireContext())
		.setPositiveButton(R.string.yes) { _, _ ->
			logEvent("event1")
		}
		.show()
}

My question is about the part

{ _, _ ->
		logEvent("event1")
}

Is that the shortest way to do it or is there some fancy syntax that I’m forgetting about? (I’m still pretty new to Kotlin.)

In this particular use case, there are limited options.
You can pass a function directly, but then its signature has to match.

If setPositiveButton would expect a function with one argument, and that argument were a string, you could do:

.setPositiveButton(R.string.yes, ::logEvent)

and logEvent would be invoked with whatever argument is passed to it.
What you need to understand about the situation is that a function is just another object, and the type of that object is effectively its signature. A function of different arity, or different argument types, or a different return type, is another type and can therefore not be passed on directly.

Now, if you are in the situation that you have to do this a lot and you don’t want to type all of that every time, you could implement the whole shebang as a class extending the function (yes, that’s a thing you can do). This would look something like this (The exact type of the arguments the lambda expects are not inferable from the code you posted, so I’ll just use ‘Any’, and UNIT for the return type):

class LoggerWrapper(message: String) : (Any, Any) -> Unit {

    override fun invoke(arg1: Any, arg2: Any) {
        logEvent(message)
    }
}

Now you could just pass an instance of that class:

.setPositiveButton(R.string.yes, LoggerWrapper("event1"))

However, A very important question in this whole thing is… Is the lambda you pass there really invoked asynchronously, or is it actually invoked synchronously? Because if it is invoked synchronously, you could just do this, and the result would pretty much be the same:

.setPositiveButton(R.string.yes)
                .also { logEvent("event1") }
		.show()