Anton Afanasev
06/04/2021, 7:16 PMexpect class MailBox(firstName: String) {}
Now, I want that my Android actual implementation will have some Android specific object passed as well. Lets say Context
I can not do
actual class MailBox actual constructor(firstName: String, context: Context) // not allowed as actual constructor does not match the expected.
If I use a secondary constructor, I cannot be sure that it will be called and therefore Context is going to be of Nullable
type and smell.
actual class MailBox actual constructor(firstName: String) {
private var context: Context? = null // Context is of Nullable - not nice
constructor(context: Context, firstName: String = "Bobby", ) : this(firstName) {
this.context = context
}
Obviously, I can declare my expected without primary constructor - but then I have no control over each platform passing a mandatory argument.
expect class MailBox {} //commonMain
actual class MailBox(context: Context, firstName: String) {} //androidMain
actual class MailBox(firstName: String) {} //iOSMain
//No ability to force passing `firstName` argument. Any platform can decide to modify/remove `firstName` on their will.
QUESTION: Are there any other, cleaner approaches that allows me to force some expected constructor values mandatory, while others can be platform specific?Dominaezzz
06/04/2021, 7:59 PMexpect
/ actual
.kevindmoore
06/04/2021, 8:20 PMrusshwolf
06/04/2021, 9:42 PMfirstName
is mandatory then maybe you want
expect class Mailbox {
val firstName
}
Then the `actual`s are forced to define it somehow, and the most natural path will be a constructor argument
actual class MailBox(firstName: String) {
actual val firstName = firstName
}
But I agree that using an interface or abstract class might be nicer for this.Anton Afanasev
06/04/2021, 10:49 PMfirstName
as "mandatory". By "mandatory" I meant that I would like to keep this value injected in the constructor no matter what platform it is. Like with expect/actual I force all the platforms to respect this protocol.
Thank you for your input