Mutli-platform expect class with companion


#1

How can I create a companion object for a class in a multi-platform build. I’d like to have the companion in the common module as it does not need any platform specific code.

// common
expect class Foo {
    fun bar()

   companion object {
       private val someString = "yeah"
       fun foobar(){
           println(someString)
           Foo().bar()
        }
    }
}
// jvm
actual class Foo {
    fun foo() { println("hi") }
}

Is there some way to do this? Right now I get a few errors:

Expected declaration can not be private
Expected declaration must not have a body

Edit:
It seems impossible to create any method or field with an implementation in an expected class. Is there any reason why I should not be able to implement some methods in the common module and some in the platform specific modules? This would save a lot of duplicate code.


#2

This isn’t supported yet, see this issue: https://youtrack.jetbrains.com/issue/KT-20427