b

    bod

    2 years ago
    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:
    expect class File(path: String) {
        constructor(path: File, name: String)
    
        fun isDirectory(): Boolean
    }
    jvm:
    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

    2 years ago
    in kotlin you have to call primary constructor in your secondary for jvm, secondary constructor is not calling primary, resulting in an error
    Kroppeb

    Kroppeb

    2 years ago
    ^
    b

    bod

    2 years ago
    but the secondary constructor exists in JVM, what's the proper syntax to just "export" it?
    s

    sikri

    2 years ago
    but it does not exist in Kotlin
    Kroppeb

    Kroppeb

    2 years ago
    Could you cange the primary to a secondary?
    s

    sikri

    2 years ago
    I doubt that this way you will store semantics
    path: File, name: String
    may not be equal to
    path.absolutePath + File.SEPARATOR + name
    tbh I would look on
    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

    2 years ago
    should I just do a wrapper
    s

    sikri

    2 years ago
    you can just copy logic of
    <http://java.io|java.io>.File
    constructor
    b

    bod

    2 years ago
    this seems to work
    expect class File {
        constructor(path: String)
        constructor(path: File, name: String)
    
        fun isDirectory(): Boolean
    }
    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
    😂
    sorry for the noise/confusion
    Dico

    Dico

    2 years ago
    Primary constructor is not mandatory.