Is there any plan to add support for different con...
# multiplatform
z
Is there any plan to add support for different constructor per platform with `expect`/`actual` mechanism? For example with Android and iOS, usually for Android there is a need to pass a context while for iOS there is no such thing. Currently I’m playing around with some
setup()
functions that initialize my object and I hide the real constructor from the client. This approach seems far from perfect though 🤔
g
I think it makes more sense to use an interface with platform specific concrete implementations in this case.
expect/actual
make it easy to use the expected class in common which won't make it possible to use in common with platform specific dependencies in constructor
👀 1
m
You can have non-expected platform-specific constructors and expected common constructors in the same class
🙏 1
😮 1
z
@mbonnin Oh, how you do non-expected platform-specific constructors?
m
Nothing specific to do, write them as you would write a regular constructor
Although TBH I found that this creates some confusion for users. Maybe it's because it's still the early days of multiplatform but having the same class callable from common code and platform-specific code feels a bit weird
z
I see, so it’s already inside of the
actual
class! It’s not really non-expected platform-specific. I think this is something worth to try! Well definitely there is some confusion in it. In my case, I just need to expose a platform specific constructor for a client and this approach along with interface might work quite well 💡 Thanks!
👍 1
g
TIL! Thanks @mbonnin
r
Here is an approach with MPP and Kodein DI -- the actual factory class either takes the context or not, and is instantiated via Kodein modules within iosMain and androidMain -- the factory is used in the common code module to bind the interface implementation: https://stackoverflow.com/questions/63020969/kotlin-mpp-expect-actual-with-different-signatures/64141659#64141659
z
@rocketraman I’m using Kodein DI too but it’s not helpful in context of exposing different constructor per platform to a client (i’m developing an sdk)
r
@zalewski.se Right, if you're developing an SDK, there isn't much you can do, nor probably should you. I think a good model to follow is SQLDelight. They simply expose platform-specific driver classes that implement an interface. No `expect`/`actual` at all.
126 Views