Skip to content

Add support for Swift to Kotlin cases #42

@rickclephas

Description

@rickclephas
Owner

Currently the whole library is focused on Kotlin to Swift cases but there are also valid cases for Swift to Kotlin support.

For example you should be able to call the following function from Swift:

suspend fun doSomething(something: suspend (String) -> String): String = TODO()

https://kotlinlang.slack.com/archives/C3PQML5NU/p1644904527044239

Activity

self-assigned this
on Feb 15, 2022
added 2 commits that reference this issue on Feb 19, 2022
added this to the v1.0 milestone on Apr 3, 2022
rickclephas

rickclephas commented on Apr 22, 2022

@rickclephas
OwnerAuthor

Some thoughts about Swift implementations of Kotlin classes/interfaces.

Kotlin code:

interface A {
    suspend fun foo(): String
    suspend fun bar(): String {
        return "Default"
    }
}

open class AKotlinImpl: A {
    suspend fun foo(): String {
        return "Kotlin"
    }
}

class AKotlinImplNoDefaults: AKotlinImpl {
    override suspend fun bar(): String {
        return "Kotlin"
    }
}

suspend fun main() {
    AKotlinImpl().run {
        println(foo()) // Kotlin
        println(bar()) // Default
    }
    AKotlinImplNoDefaults().run {
        println(foo()) // Kotlin
        println(bar()) // Kotlin
    }
}

Generated Kotlin code:

abstract class ANativeImpl: A {
    final override suspend fun foo(): String { /* ... */ }
    abstract fun fooNativeImpl(): NativeSuspend<String>

    final override suspend fun bar(): String { /* ... */ }
    open fun barNativeImpl(): NativeSuspend<String> { /* ... */ }
}

fun A.fooNative(): NativeSuspend<String> { /* ... */ }
fun A.barNative(): NativeSuspend<String> { /* ... */ }

Swift code:

class ASwiftImpl: ANativeImpl {
    override func fooNativeImpl() -> NativeSuspend<String> {
        return nativeSuspend {
            return "Swift"
        }
    }
}

class ASwiftImplNoDefaults: ASwiftImpl {
    override func barNativeImpl() -> NativeSuspend<String> {
        return nativeSuspend {
            return "Swift"
        }
    }
}

func main() async {
    let impl = ASwiftImpl()
    print(await asyncFunction(for: impl.fooNative())) // Swift
    print(await asyncFunction(for: impl.barNative())) // Default
    let implNoDefaults = ASwiftImplNoDefaults()
    print(await asyncFunction(for: implNoDefaults.fooNative())) // Swift
    print(await asyncFunction(for: implNoDefaults.barNative())) // Swift
}

Note: this won't work because A is an interface and doesn't expose the extension functions like this.
Support for interfaces is limited anyway because ObjC doesn't support default implementations.

Note: this way calling the Swift implementations from Swift will convert from async to NativeSuspend and back to async.
To fix that we would need an extension/wrapper function in Swift that can directly call the async version instead.
That would need some kind of Swift codegen (during Kotlin compile) to make this easy to use.

chris-hatton

chris-hatton commented on Jul 28, 2023

@chris-hatton

Just dropping a note to say I too arrived here looking for a library to help with consuming a Swift ASyncStream from Kotlin/Native.

added a commit that references this issue on Aug 20, 2023
added a commit that references this issue on Nov 18, 2023
linked a pull request that will close this issueAdd support for coroutine parameters #152on Nov 19, 2023
davidgarywood

davidgarywood commented on Jul 18, 2024

@davidgarywood

Just dropping a note to say I too arrived here looking for a library to help with consuming a Swift ASyncStream from Kotlin/Native.

Me too! Does anything like this exist ?

added a commit that references this issue on Aug 3, 2024
added a commit that references this issue on Aug 11, 2024
added a commit that references this issue on Sep 29, 2024
l2hyunwoo

l2hyunwoo commented on Feb 6, 2025

@l2hyunwoo

Hello, any updates in here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

    Development

    Participants

    @chris-hatton@davidgarywood@rickclephas@l2hyunwoo

    Issue actions

      Add support for Swift to Kotlin cases · Issue #42 · rickclephas/KMP-NativeCoroutines