hey any ideas how to achieve kotlin data class wit...
# multiplatform
a
hey any ideas how to achieve kotlin data class with default arguments like
Copy code
data class TestViewState(
    val list: List<Test> = emptyList(),
    val stateText: String = ""
)
so on XCode it would have default empty constructor? like
Copy code
@State private var viewState: TestViewState = TestViewState()
Currently it is not working because of XCode error :
'init()' is unavailable
It does work as long as it is initialised with values Sorry I am Android dev, not iOS 🙂 (edited) Also made it happen with adding getter to companion object, but this is a hack not a solution
v
I answered here https://kotlinlang.slack.com/archives/C0B8M7BUY/p1666436627010159?thread_ts=1666435668.224729&amp;cid=C0B8M7BUY It's just a language, not really related to multiplatform.
a
yep, sorry, that was wrong channel. You answered with swift code, but what I need is kotlin data class usage on swift, not creating swift class
v
As far as I know, default arguments (wheter it is methods or constructors) are not available in objC. Either you explicitly pass them from iOS, you create secondary constructor or you could do a wrapper in
iosMain
s
I'm unable to find it right now but there is a youtrack for this
a
I did workaround like this:
Copy code
data class TestViewState(
    val list: List<Test> = emptyList(),
    val stateText: String = ""
) {
    
    companion object {
        val initialise get() = TestViewState()
    }
}
and in swift
Copy code
@State private var viewState: TestViewState = TestViewState.Companion().initialise
d
I find this annoying. I fix this by replicating the constructor:
Copy code
data class TestViewState(
    val list: List<NavEvent.Test> = emptyList(),
    val stateText: String = ""
) {
    
    constructor() : this(
        list = emptyList(),
        stateText = ""
    )
}
a
replicating constructor looks better, thanks
d
The YouTrack issue for this is https://youtrack.jetbrains.com/issue/KT-38685
s
Btw, there is no need to specify all the arguments when replicating the constructor. Something like
Copy code
constructor() : this(list = emptyList())
should be enough.