I have a situation where I need to define a static property on a class in Kotlin and when its compiled to Javascript have it become a true static field on that class. In this situation companion objects do not work.
For example, if I have an abstract class and its implementing class like below:
abstract class MyAbstractClass{
abstract val id: Int
}
class MyClass: MyAbstractClass(){
override val id: Int = 1 //I want this to actually be "static" on the MyClass
}
The Javascript that this compiles down to is this:
function MyAbstractClass() {
}
function MyClass() {
MyAbstractClass.call(this);
this.id_jz5fma$_0 = 1;
}
Object.defineProperty(MyClass.prototype, 'id', {
get: function () {
return this.id_jz5fma$_0;
}
});
But what I need it to compile down to is this:
function MyAbstractClass() {
}
function MyClass() {
MyAbstractClass.call(this);
}
MyClass.id = 1;
So that the id
field does actually statically exist on MyClass
without having to make a new instance of MyClass
.
I've tried using a companion object but that creates a separate object/function called MyClass$Companion
and then assigns the id
field to that and never actually assigns it statically to MyClass
.
How can I go about setting true static fields like this in Kotlin?