Conflicting overloads @ObjCConstructor with typealias NSURL


#1

Hi,

Kotlin: 1.2.31
Kotlin-Native: 0.6.2

I’m starting to use Kotlin Multiplatform features and I’m having trouble defining typealias for iOS native module.

I want to have an expect class URL in my common module and use platform specific classes in each module:

JVM:

import java.net.URL

actual typealias URL = URL

iOS-native:

import platform.Foundation.*

actual typealias URL = NSURL

In Java it compiles without problems, but on iOS it’s having trouble with this error:

[system.err] .../URL.kt:5:1: error: conflicting overloads: @ObjCConstructor public final actual fun <init>(path: String, relativeToURL: NSURL?): URL /* = NSURL */ defined in com.climbaround.common.types.URL, @ObjCConstructor public final actual fun <init>(path: String): URL /* = NSURL */ defined in com.climbaround.common.types.URL, @ObjCConstructor public final actual fun <init>(URLString: String): URL /* = NSURL */ defined in com.climbaround.common.types.URL, @ObjCConstructor public final actual fun <init>(URLString: String, relativeToURL: NSURL?): URL /* = NSURL */ defined in com.climbaround.common.types.URL, @ObjCConstructor public final actual fun <init>(data: NSData, relativeToURL: NSURL?): URL /* = NSURL */ defined in com.climbaround.common.types.URL, @ObjCConstructor public final actual fun <init>(data: NSData, relativeToURL: NSURL?): URL /* = NSURL */ defined in com.climbaround.common.types.URL
[system.err] actual typealias URL = NSURL
[system.err] ^

I haven’t found any documentation about how to solve this.

Thanks a lot.


#2

Do you use ‘-produce framework’ mode?


#3

Also see https://github.com/JetBrains/kotlin-native/issues/1444 for some context.


#4

Kotlin classes aren’t allowed to have multiple constructors with the same signature (i.e. types of parameters).
But Objective-C classes can have such constructors.
When defining actual typealias URL = NSURL, the compiler tries to create Kotlin URL declaration but can’t due to clashing constructors.
This problem can be workarounded by adding @Suppress annotation:

@Suppress("CONFLICTING_OVERLOADS") actual typealias URL = platform.Foundation.NSURL

#5

Also please note that this code can’t be compiled yet to framework anyway, see the link posted above by @Nikolay.Igotti


#6

So it’s not possible to use NSURL as actual typealias in a framework for iOS?

I think this limits a lot sharing code with Android and iOS. May we have to wait a little bit more?


#7

It will be possible soon.


#8

Great, that will allow sharing better and more complex data structures.

Thanks a lot