closure/lambda 的 Kotlin 命名参数语法

Kotlin named parameter syntax for closure/lambda

我创建了以下函数:

public fun storeImage(image: BufferedImage, toPath: String, 
    onCompletion: (contentURL: URL) -> Unit)
{
    val file = File(this.storageDirectory, toPath)
    log.debug("storing image: ${file.absolutePath}")
    val extension = toPath.extensionOrNull()
    if (!file.exists())
    {
        file.parentFile.mkdirs()
        file.createNewFile()
    }
    ImageIO.write(image, extension!!, FileOutputStream(file.absolutePath))
    onCompletion(URL(contentBaseUrl, toPath))
}

我看可以这样调用:

contentManager.storeImage(image, "1234/Foobar.jpg", onCompletion = {
    println("$it")
})

或者我可以使用尾随闭包语法:

contentManager.storeImage(image, "1234/Foobar.jpg") {
    println("$it")
}

但是如何调用存储图像方法和使用命名参数调用onCompletion 函数?

Edit/Example:

我想使用类似于以下的语法调用 storeImage 方法:

contentManager.storeImage(image, "1234/Foobar.jpg", 
    onCompletion = (bar: URL) : Unit -> {
       //do something with bar
    }

我在文档中找不到上述内容的正确语法。

您可以使用常规语法为 lambda 参数命名。无论您是否使用命名参数将 lambda 传递给函数,这都有效。

contentManager.storeImage(image, "1234/Foobar.jpg", 
    onCompletion = { bar ->
       //do something with bar
    })