Request for community comments: Better Kotlin support
See original GitHub issueWith an awesome I/O17 being over, one of the big announcements were official support for Kotlin.
Realm is already fully interoperable with Kotlin and we have a Kotlin example here and here that demonstrates how it works.
But we want to make that support even better and In order to do that, we are looking for feedback on what you would most like to see changed so Realm works even better with Kotlin
We have a few ideas ourselves:
-
(DONE): Add extension functions for all classes with methods that currently accept
Class<? extends RealmModel>
soKClass
can be used directly, e.g.realm.where(Person::class)
instead ofrealm.where(Person::class.java)
. -
(DONE) Detect nullability automatically in model classes. Right now you are required to use the Realm
@Required
annotation, but we can add support for JetBrains@NotNull
as well which is how the Kotlin bytecode indicate not-null values. Examplevar name : String
instead of of@Required var name : String
-
(DONE) Annotate our public API with JSR305 annotations in a similar way to Okio/OKHttp. This means that Realm will expose correctly the nullability of return values and input paramaters. For example:
val result : RealmResults<Person> = realm.where(Person::class).findAll()
instead ofval results : RealmResults<Person>? = realm.where(Person::class).findAll()
. Also being tracked here: https://github.com/realm/realm-java/issues/4643
But we are very interested what problems you are running into when using Realm from Kotlin.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:26 (17 by maintainers)
Another Kotlin fun fact:
Long::class.java
actually returns a Class reference tolong
notLong
, this is really easy mistaken when writing migrations:Even better would be supporting the KClass equivalents directly:
🎊 Kotlin!!! 🎊
When you say problems you are having with Realm using Kotlin, I do not have many problems. realm-java works really well with Kotlin at the moment. I have a few apps that are 100% Kotlin and Realm code bases. Works great.
My vote for priority is your list item #2. Here is an example of a Realm model I create in a Kotlin app using Kotlin nullability data type in them for optional fields:
So not needing annotations and instead using the data types of my Kotlin properties would be awesome.