9

I created the following function:

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

I can see that I can call it like this:

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

Or I can use trailing closure syntax:

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

But how do I call the store image method and call the onCompletion function using named parameters?

Edit/Example:

I would like to call the storeImage method using a syntax similar to:

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

I could not find the correct syntax in the docs for the above kind of thing.

8
  • To be clear, with "named parameters" you mean explicitly assigning your contentURL param? Because with the onCompletion = ... thing you're already using named parameters. Commented Jan 6, 2016 at 8:43
  • Yes, that what I mean. It is a very simple (but poorly worded, will try to fix) question. Commented Jan 6, 2016 at 8:45
  • 1
    Interesting corner case there. You're not allowed to, the compiler says "Named parameters are not allowed for function types". I've left a message for the kotlin people and let's see if we can get an answer as to why that's not possible. Commented Jan 6, 2016 at 8:58
  • Ah that's interesting. If I drop onCompletion = can i refer to the function parameter as 'contentURL' instead of 'it' ? Commented Jan 6, 2016 at 9:03
  • 1
    I think you're confusing two things: declaration of onCompletion (in the storeImage fun) can have named params "for documentation". In the definition of onCompletion you'd still need to use the closure syntax: onCompletion = { contentURL -> /* do stuff */ }. On the call site, you're not allowed to name the param: onCompletion(URL(...)). Commented Jan 6, 2016 at 9:14

1 Answer 1

13

You can use the regular syntax for giving names to lambda parameters. This works regardless of whether you're using a named argument to pass the lambda to the function.

contentManager.storeImage(image, "1234/Foobar.jpg", 
    onCompletion = { bar ->
       //do something with bar
    })
Sign up to request clarification or add additional context in comments.

1 Comment

I added an IntelliJ feature suggestion, for consideration: youtrack.jetbrains.com/issue/IDEA-150085

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.