Open Burtan opened 2 years ago
Hi @Burtan. We will try to devise a fix for this problem since the code you wrote is syntactically correct and the error appears to be stemming from our compiler plugin.
Though I have an observation about the class definition you wrote. I assume you want to create an inheritance hierarchy by writing, say
open class NewBaseEntity<T> : RealmObject // I added the `open` modifier myself
class Foo : NewBaseEntity<String>()
While the class declaration you wrote and the one above are syntactically correct, inheritance/polymorphism is not supported by Realm yet and therefore you will not be able to use this in your schema - see https://github.com/realm/realm-java/issues/761 for more info. This constraint from our SDK would render your problem moot (if this is in fact what you are trying to do) as you would not be able to use said hierarchies in Realm.
I think my case is a little special. I'm using multiple interfaces on my entity classes
class A: RealmObject, Bable<T>, Cable<T>, Dable<T>
To use generics on a combination of these interfaces I need a common interface that I don't inherit from.
MetaInterface<T>: RealmObject, Bable<T>, Cable<T>
fun doSomething<A: MetaInterface<T>>()
Hi @Burtan.
Even though you are not using MetaInterface
actively for inheritance purposes, our compiler plugin visits all model definitions implementing RealmObject
and their fields to generate the code that allows us to create your database schema when you open a realm. This will ultimately fail as models implementing RealmObject
need to have a zero-argument constructor for us to be able to instantiate them as managed Realm objects. Since interfaces do not have such a constructor we reject them as invalid - we do not support inline instantiation as we would not be able to handle compile time-defined properties in the schema. These constraints are established by Realm Core because they do not support polymorphic inheritance so we cannot lift this requirement yet.
Additionally:
// currently not working, requires a bug fix
class MyClass<T>: RealmObject, Bable<T>, Cable<T>, Dable<T> {
@io.realm.kotlin.types.annotations.Ignore
var myGenericField: T? = null // MUST BE IGNORED as this is a dynamic type and we cannot generate appropriate schema creation instructions in compile time
}
should be allowed as long as your Xable<T>
interfaces do not inherit from RealmObject
and you ignore any generic fields - see comment in the snippet.
Something along these lines should also be allowed, again, as long as you don't make your interface inherit from RealmObject
:
interface MyInterface<T> {
var genericField: T?
}
// currently not working, requires a bug fix
class MyGenericClass<T> : RealmObject, MyInterface<T> {
@io.realm.kotlin.types.annotations.Ignore
override var genericField: T? = null // MUST be ignored, same reason as above
}
class MyStringClass : RealmObject, MyInterface<String> {
override var genericField: String? = null // OK
}
We will try to devise a bug fix for class MyClass<T>: RealmObject
.
Thanks for the effort and extensive explanation already!
Hi,
when using generics on an interface/class that extends RealmObject you get an error. Not sure if it is kotlin or realm though.
class BaseEntity<T> : RealmObject