Multiplatform with platform specific overrides, but also common fallbacks

Is it possible to have a class implemented in the common source set, while
also overriding it in a specific language.

For example, if I’m implementing a Base64 encoder/decoder. I have a default, multiplatform implementation in the common source set. However, as Java as its own Base64 implementation,
which is arguably much better optimized than mine, I’d like to add that as an actual override for
my multiplatform implementation, while still having my implementation as a fallback for e.g. the JS platform.

The only way I can figure is to use an expect declaration in the common source set, and then an actual declaration in the JVM source set using Java’s encoder, and then copy my fallback implementation into every other source set as the actual declaration. But this sounds like an absolutely terrible idea and a nightmare to maintain.

Any solutions?

I’m pretty sure that this is not yet possible. Here is the youtrack issue for this feature: https://youtrack.jetbrains.com/issue/KT-20427

Thank you so much. That is exactly what I was looking for.

I was unsure if an issue already existed for this, but I’ll guess I’ll have to wait until the feature is released.

Thank you nevertheless.

You could also have the common implementation in common under a different name (e.g. Base64DecoderDefault) and then when you have a platform that doesn’t have a native implementation then point your actual declaration to a typealias of the Base64DecoderDefault

2 Likes