Adam S
05/11/2024, 11:44 AMexpect class
can't have function bodies. So I'd need to create a common class for the shared functions and re-use it in each `actual class`:
// commonMain
expect class MyDecoder {
fun decode(string: String): Node // bodies are not allowed :(
}
internal class MyDecoderCommon {
fun decode(string: String): Node { /* impl*/ }
}
// jvmMain
actual class MyDecoder {
private val common = MyDecoderCommon()
fun decode(string: String): Node = common.decode(string)
fun decode(inputStream: InputStream): Node = decode(inputStream.toString())
}
Is this the best pattern available at the moment? I'm not a fan because I get dinged with a 'expect/actual classes are in beta' warning, and it's a bit duplicated.
(Making MyDecoder
a regular class and adding extension functions in jvmMain, like fun MyDecoder.decode(inputStream: InputStream): Node
are also an option - however I want to support Java users)Oliver.O
05/11/2024, 8:51 PMopen class MyDecoder
(with implementation) in commonMain
plus class MyJvmDecoder : MyDecoder
in jvmMain
work for you? (Because with the extra decode
overload it really is a derivative, not the same thing as MyDecoder
).Michael Krussel
05/13/2024, 1:05 PMfun decode(InputStream)
I would just make that an extension function.Adam S
05/15/2024, 6:24 AMMichael Krussel
05/15/2024, 12:19 PM@fileJvmName
to give a good name to the class containing the extension functions would create a natural class of static functions for your Java users. Not quite as good extension functions, but it is the normal Java way of doing it.
But other wise I think what you did is a fine implementation. I've done similar with just internal functions that I expect most of the actual classes to use.