Search code examples
kotlinkotlin-multiplatformkotlin-native

Kotlin/Native interoperability with Swift: generated interface with two methods having same signature


I have a Kotlin multiplatform project which performs Bluetooth operations on iOS devices using the CoreBluetooth library. I am having some problems with getting the peripheral disconnected callback. On closer inspection, I see that the generated CBCentralManagerDelegateProtocol interface (which I override) has two methods with same signature.

@kotlin.commonizer.ObjCCallable public open expect fun centralManager(central: platform.CoreBluetooth.CBCentralManager, didFailToConnectPeripheral: platform.CoreBluetooth.CBPeripheral, error: platform.Foundation.NSError?): kotlin.Unit { /* compiled code */ }

@kotlin.commonizer.ObjCCallable public open expect fun centralManager(central: platform.CoreBluetooth.CBCentralManager, didDisconnectPeripheral: platform.CoreBluetooth.CBPeripheral, error: platform.Foundation.NSError?): kotlin.Unit { /* compiled code */ }

So, I can override only one of these methods. My question is am I missing something here? Like a way to override both the methods.


Solution

  • In kotlin you can't declare two functions with the same signature, differing only by argument names. But in ObjC you can.

    To support this interop you can use @Suppress("CONFLICTING_OVERLOADS"), as described here in documentation.

    To override different methods with clashing Kotlin signatures, you can add a @Suppress("CONFLICTING_OVERLOADS") annotation to the class.

    @Suppress("CONFLICTING_OVERLOADS", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
    override fun centralManager(
        central: CBCentralManager,
        didFailToConnectPeripheral: CBPeripheral,
        error: NSError?
    ) {
    }
    
    @Suppress("CONFLICTING_OVERLOADS", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
    override fun centralManager(
        central: CBCentralManager,
        didDisconnectPeripheral: CBPeripheral,
        error: NSError?
    ) {
    }