https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
f

Florian Klein

11/04/2023, 12:41 PM
Dear community! Is there a way to implement an interface like platform.WebKit.WKNavigationDelegateProtocol in kotlin where method have the same signature? I'm struggling with this error: error: Overload resolution ambiguity: public open fun webView(webView: WKWebView, didCommitNavigation: WKNavigation?): Unit defined in platform.WebKit.WKNavigationDelegateProtocol public open fun webView(webView: WKWebView, didFinishNavigation: WKNavigation?): Unit defined in platform.WebKit.WKNavigationDelegateProtocol public open fun webView(webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation: WKNavigation?): Unit defined in platform.WebKit.WKNavigationDelegateProtocol public open fun webView(webView: WKWebView, didStartProvisionalNavigation: WKNavigation?): Unit defined in platform.WebKit.WKNavigationDelegateProtocol Thanks in advance!
r

russhwolf

11/04/2023, 2:06 PM
You can suppress the error with
@Suppress("CONFLICTING_OVERLOADS")
(as recommended here)
a

Alex Azarov

11/04/2023, 7:19 PM
If my understanding is right it will only fix the build error, however, the compiler still won’t be able to distinguish between signatures resulting in a random implementation called each time.
r

russhwolf

11/04/2023, 7:20 PM
It will be able to distinguish them if you pass parameter names
a

Alex Azarov

11/04/2023, 7:24 PM
In this case, it’s the iOS that calls these methods, do you still think it’s safe to use? I don’t quite understand what documentation means by
Stability isn’t guaranteed in this case so use it with caution.
r

russhwolf

11/04/2023, 7:28 PM
I take that to mean API stability across future versions. You might run into issues when updating Kotlin or interacting with code built with a different Kotlin version
They're reserving the possibility of breaking things so it's not part of "stable" kmp. Similar to other interop stuff that has @ExperimentalForeignApi annotations because it's not being stabilized yet
f

Florian Klein

11/04/2023, 7:34 PM
Thanks for the response!
I just tested to use the suppress annotation like this: object WKDelegate : NSObject(), WKNavigationDelegateProtocol { @Suppress("CONFLICTING_OVERLOADS") override fun webView(webView: WKWebView, didCommitNavigation: WKNavigation?) { } @Suppress("CONFLICTING_OVERLOADS") override fun webView(webView: WKWebView, didFinishNavigation: WKNavigation?){ } @Suppress("CONFLICTING_OVERLOADS") override fun webView(webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation: WKNavigation?) { } @Suppress("CONFLICTING_OVERLOADS") override fun webView(webView: WKWebView, didStartProvisionalNavigation: WKNavigation?) { } } But it's still not compiling now getting a different error.
Turns out it actually has to be a "class" and can't be an "object"! 🤔
2 Views