From af83c5acef60015e53d4f2b76dc018be82a98846 Mon Sep 17 00:00:00 2001 From: "Danil.Pavlov" Date: Thu, 14 Aug 2025 13:09:12 +0200 Subject: [PATCH] feat: block parameter names --- docs/topics/native/native-objc-interop.md | 35 +++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/topics/native/native-objc-interop.md b/docs/topics/native/native-objc-interop.md index ff817827e1a..42ac105cd0b 100644 --- a/docs/topics/native/native-objc-interop.md +++ b/docs/topics/native/native-objc-interop.md @@ -512,7 +512,7 @@ The same is true for `MutableMap`. ### Function types -Kotlin function-typed objects (for example, lambdas) are converted to functions in Swift and blocks in Objective-C. +Kotlin function-typed objects (for example, lambdas) are converted to closures in Swift and blocks in Objective-C. [See an example of a Kotlin function with a lambda in the Kotlin-Swift interopedia](https://github.com/kotlin-hands-on/kotlin-swift-interopedia/blob/main/docs/functionsandproperties/Functions%20returning%20function%20type.md). However, there is a difference in how types of parameters and return values are mapped when translating a function and a @@ -534,13 +534,44 @@ func foo(block: (KotlinInt) -> KotlinUnit) And you can call it like this: -```kotlin +```swift foo { bar($0 as! Int32) return KotlinUnit() } ``` +#### Explicit names in block types + +You can add explicit names to Kotlin's block types for exported Objective-C headers. Without them, Xcode's autocompletion +suggests calling such functions with no names in the block, and the generated block triggers Clang warnings. + +To enable block parameter names, add the following [binary option](native-binary-options.md) to your `gradle.properties` file: + +```none +kotlin.native.binary.objcExportBlockExplicitParameterNames=true +``` + +For example, for the following Kotlin code: + +```kotlin +// Kotlin: +fun greetUser(block: (name: String) -> Unit) = block("John") +``` + +Kotlin forwards the parameter names from Kotlin function types to Objective-C block types, so Xcode uses them in suggestions: + +```objc +// Objective-C: +greetUserBlock:^(NSString *name) { + // ... +}; +``` + +> This option affects Objective-C interop only and generally doesn't change behavior when calling Kotlin code from Swift. +> +{style="note"} + ### Generics Objective-C supports "lightweight generics" defined in classes, with a relatively limited feature set. Swift can import