realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

@Ignore and kotlin property delegate #2921

Open beeender opened 8 years ago

beeender commented 8 years ago
open class EnabledEngine : RealmObject {
    //@Ignored cannot be applied here
    val engine: SearchEngine by lazy {
        searchEngines[id]!!
    }
}

Without @Ignore a build error will occur since final property cannot pass annotation process

marchy commented 7 years ago

This is a huge limitation. It seems you cannot use delegated properties with ANY Realm properties – making Realm effectively incompatible with Kotlin.

Not only do Realm annotations not work on (Annotation processor error: "This annotation is not applicable to target 'member property with delegate'"), but because Delegated properties are themselves final (the delegated property proxy object does not itself get changed after initial assignment), Realm throws annotation processing error: "Final fields are not allowed. Class: Chat, Field: messages$delegate"

How are you supposed to do custom setters on Realm objects?

reacuna commented 7 years ago

As @davidgarciaanton mentioned in https://github.com/realm/realm-java/issues/4185, you can use @delegate:Ignore like this:

open class EnabledEngine : RealmObject {
    @delegate:Ignore
    val engine: SearchEngine by lazy {
        searchEngines[id]!!
    }
}

Check the Kotlin docs on Annotation Use-site Targets

sheinin commented 1 year ago

Not working in the latest with "'@delegate:' annotations could be applied only to delegated properties". Any workaround for this?