https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
b

bod

07/11/2020, 1:16 PM
Hello, World! I'm having trouble finding the correct syntax to declare an
expect
class and reflecting the constructors of the
actual
one. Here's what I'm trying to do: common:
Copy code
expect class File(path: String) {
    constructor(path: File, name: String)

    fun isDirectory(): Boolean
}
jvm:
Copy code
actual class File actual constructor(path: String): java.io.File(path) {
    actual constructor(path: File, name: String)
}
This results in
Primary constructor call expected
s

sikri

07/11/2020, 1:19 PM
in kotlin you have to call primary constructor in your secondary for jvm, secondary constructor is not calling primary, resulting in an error
k

Kroppeb

07/11/2020, 1:19 PM
^
b

bod

07/11/2020, 1:20 PM
but the secondary constructor exists in JVM, what's the proper syntax to just "export" it?
s

sikri

07/11/2020, 1:20 PM
but it does not exist in Kotlin
k

Kroppeb

07/11/2020, 1:21 PM
Could you cange the primary to a secondary?
s

sikri

07/11/2020, 1:21 PM
I doubt that this way you will store semantics
Copy code
path: File, name: String
may not be equal to
path.absolutePath + File.SEPARATOR + name
tbh I would look on
Copy code
companion object {
    operator fun invoke(path: String)
    operator fun invoke(path: File, name: String)
}
but I don’t know if it is possible to have
expect/actual companion object
b

bod

07/11/2020, 1:25 PM
should I just do a wrapper
s

sikri

07/11/2020, 1:26 PM
you can just copy logic of
<http://java.io|java.io>.File
constructor
b

bod

07/11/2020, 1:28 PM
this seems to work
Copy code
expect class File {
    constructor(path: String)
    constructor(path: File, name: String)

    fun isDirectory(): Boolean
}
Copy code
actual class File : java.io.File {
    actual constructor(path: String):super(path)
    actual constructor(path: File, name: String):super(path, name)
}
wait I think all I wanted was:
actual typealias File = <http://java.io|java.io>.File
😂
😅 2
sorry for the noise/confusion
d

Dico

07/11/2020, 10:37 PM
Primary constructor is not mandatory.
2 Views