question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request for community comments: Better Kotlin support

See original GitHub issue

With 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:

  1. (DONE): Add extension functions for all classes with methods that currently accept Class<? extends RealmModel> so KClass can be used directly, e.g. realm.where(Person::class) instead of realm.where(Person::class.java).

  2. (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. Example var name : String instead of of @Required var name : String

  3. (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 of val 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:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:26 (17 by maintainers)

github_iconTop GitHub Comments

11reactions
cmelchiorcommented, Jun 9, 2017

Another Kotlin fun fact:

Long::class.java actually returns a Class reference to long not Long, this is really easy mistaken when writing migrations:

realm?.schema?.create("Person")
        ?.addField("id", Long::class.java) // Non-nullable

// Instead we should document that people need to use these instead
.addField("id", Long::class.javaObjectType) // Nullable
.addField("id", Long::class.javaPrimitiveType) // Non-nullable

Even better would be supporting the KClass equivalents directly:

.addField("id", Long::class) // Non-nullable
.addField("id", Long::class).setNullable("id") // Being nullable is hard in Kotlin
4reactions
levibostiancommented, May 24, 2017

🎊 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:

open class FooModel(@PrimaryKey override var realm_id: Int = 0,
                      @SerializedName("id") override var api_id: Int = 0,
                      var name: String? = null,
                      var created_at: Date = Date(),
                      var updated_at: Date = Date(),
                      var position: Int? = null,
                      var bar: BarModel? = null): RealmObject() 

So not needing annotations and instead using the data types of my Kotlin properties would be awesome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin Language Specification — Request for Comments
As the name suggests, the specification aims to provide a more detailed description of how Kotlin works than the current language documentation.
Read more >
Send me all the Kotlin questions you were too afraid to ask
So don't hesitate, if you have any Kotlin-related questions you were too afraid to ask, reply to this post. Top comments (10) ...
Read more >
Introducing Kotlin support in Spring Framework 5.0
A key building block of our Kotlin support is Kotlin extensions. They allow to extend existing APIs in a non-intrusive way, providing a...
Read more >
How does the Java community feel about Kotlin? - Reddit
173 votes, 226 comments. In my organization, we switch between Java and kotlin quite frequently for backend development.
Read more >
Using Kotlin - Quarkus
Quarkus provides first class support for using Kotlin as will be explained in this ... For more information about how to install the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found