Patrik Aradi
09/23/2026, 12:58 PM// kotlin interface
interface UserRepository {
val signedInUser: Flow<String>
suspend fun signInUser(user: String)
}
// generated protocol
public protocol UserRepository: KotlinRuntime.KotlinBase, ExportedKotlinPackages.com.example.swift_export._UserRepository {
var signedInUser: any KotlinCoroutineSupport.KotlinTypedFlow<Swift.String> {
get
}
func signInUser(
user: Swift.String
) async throws -> Swift.Void
}
Let's say on android I would just implement this with a MutableStateFlow, how could I implement this in swift. How can I create a KotlinTypedFlow from the Swift side?
What should I pay attention to in terms of cancellation? Will throwing a CancellationError from swift cancel the coroutine in kotlin?Guilherme Delgado
09/23/2026, 1:50 PMPatrik Aradi
09/23/2026, 2:38 PMGuilherme Delgado
09/23/2026, 2:58 PMegorand
09/24/2026, 2:04 PMArtem Olkov
09/24/2026, 2:45 PMPatrik Aradi
10/09/2026, 9:30 AMArtem Olkov
10/09/2026, 5:19 PMfinal class SwiftUserRepository: KotlinBase, UserRepository {
private let user = mutableStateFlow(initial: "")
var signedInUser: any KotlinTypedFlow<String> { user }
func signInUser(user: String) async throws {
try await Task.sleep(for: .milliseconds(300))
self.user.value = user
}
}
The only thing that is needed on the kotlin side - is this helper, to specify the generic type that will be used, as we Swift Export don't support generics ATM 🙂
fun mutableStateFlow(initial: String): MutableStateFlow<String> = MutableStateFlow(initial)
This snippet works with Kotlin 2.5.0-Beta2 and newer. For earlier kotlins one would have to do the trick with an empty class definition to inherit from it.Artem Olkov
10/09/2026, 5:22 PMWill throwing aYes, throwing a Cancellation fromfrom swift cancel the coroutine in kotlinCancellationError
signInUser would cancell kotlin counter partGuilherme Delgado
10/09/2026, 6:17 PM