Koltin multiplatform constructor/function default arguments for Swift

class GetDetailsAction(private val repository: Repository = DefaultRepository()) {
// code here
}

It would be great to be to use default arguments in constructor/function in Kotlin Multiplatform common code and be able to replace them in Swift.
I know that we don’t have default arguments in Objective C framework that is used in Swift, in this case maybe we can use something like @SwiftOverloading annotation like for JVM or something different?
For now I should write additional constructors and function in common code and it’s a lot of code…

And the questions are:

  1. It is planned to add a more beautiful solution than manual writing overloaded constructor / function?
  2. What is the best solution for handling optional params in Kotlin Multiplatform project for Android / iOS / Web platforms for now?

Thanks :slightly_smiling_face:

4 Likes

Old thread, but I was wondering the same thing. An easy solution I came up with is just:

class GetDetailsAction(private val repository: Repository = DefaultRepository()) {
    companion object {
        fun getInstance() = GetDetailsAction()
    }
}

and calling GetDetailsAction.companion.getInstance() in swift

I don’t think there’s a built in way. Correct me if I’m wrong

2 Likes