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

adam-mcneilly

09/25/2019, 3:21 PM
Is there a way to have something in the common folder that has default arguments, and just be called from iOS with a default constructor? When I try this, Xcode keeps telling me
init()
not allowed, and I need to add all params. So far the only work around I have is using a secondary constructor like this. It's not the worst thing but it feels kind of unnecessary:
Copy code
class MyClass(
	private val paramOne: Int = 5,
	private val paramTwo: Int = 10
) {
	constructor() : this(5, 10)
}
k

Kris Wong

09/25/2019, 3:23 PM
Obj-C doesn't support default arguments
K/N would need to generate bindings in swift for that to work
a

adam-mcneilly

09/25/2019, 3:24 PM
So the secondary constructor is necessary to do what I want here? I was wondering if there might be an annotation that could do it, similar to
@JvmOverloads
.
s

Sam

09/25/2019, 3:25 PM
That is the way that I do it too. It probably has to do with how default arguments are treated in exports. I'm guessing that generating all of the variations of constructors/default arguments for ObjC would be ugly.
a

adam-mcneilly

09/25/2019, 3:26 PM
Yeah I can imagine it gets messy quickly. I think this is fine because I don't need it for every class. It's also a pain because I want to inject a coroutine scope (though maybe I don't really need to do that) and I can't define it from iOS. If they really were ints, maybe I would just supply the defaults from there and call it a day.
Is it safe to assume I'll run into this with methods too? It's not just class signatures?
r

russhwolf

09/25/2019, 9:19 PM
Correct. No default args in any context get translated from Kotlin to Swift
👍 1
2 Views