Passing lambda instead of interface

As @zsmb13 said, SAM conversions are only supported for Java interfaces.

You could create an extension function to make it work though:

// Assuming the type of dataManager is DataManager.
fun DataManager.createAndSubmitSendIt(title: String, 
                                      message: String, 
                                      progressListener: (Long) -> Unit) {
    createAndSubmitSendIt(title, message,
        object : ProgressListener {
            override fun transferred(bytesUploaded: Long) {
                progressListener(bytesUploaded)
            }
        })
}

Edit

Kotlin 1.4 introduced function interfaces that enable SAM conversions for interfaces defined in Kotlin. This means that you can call your function with a lambda if you define your interface with the fun keyword. Like this:

fun interface ProgressListener {
    fun transferred(bytesUploaded: Long)
}

Leave a Comment