objectbox / objectbox-java

Android Database - first and fast, lightweight on-device vector database
https://objectbox.io
Apache License 2.0
4.41k stars 304 forks source link

Support Kotlin inline classes for ids #706

Open Debrugger opened 5 years ago

Debrugger commented 5 years ago

Inline classes are a good way to add compile time type safety to database queries. They would prevent error caused by querying a box with the wrong id type.

@Entity
data class EntityA(
    val str: String,
    val id: EntityAId
)

inline class EntityAId(val value: Long)

@Entity
data class EntityB(
    val str: String,
    val id: EntityBId
)

inline class EntityBId(val value: Long)

val unsafeId: Long = getSomeId() //returns an id meant for EntityA
boxStore.boxFor<EntityB>()[unsafeId] //Runtime error

val safeId: EntityAId = getSomeId()
boxStore.boxFor<EntityB>()[safeId] //Compile time error

And the unsafe call only crashes if you're lucky, other times it might just seem like the database is returning garbage data, since you queried it with the wrong ids.

greenrobot-team commented 5 years ago

Thanks for the suggestion. Instead of changing the type of id, which I think is probably an extreme change, did you look into solving this using extension functions? -Uwe

Delsart commented 2 months ago

koltin value class provied a way of zero cost abstraction, is there still any plan to support kotlin value class?

greenrobot-team commented 2 months ago

@Delsart This is unrelated. Answered in #1189. I'll hide our comments to not confuse other users.