objectbox / objectbox/objectbox-java

Support Kotlin inline classes for ids

Open
#706 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
4.6k
Forks
311
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the Kotlin examples using inline classes such as EntityAId and EntityBId, and trace the boxStore.boxFor()[id] call. Done means supported inline-class ids preserve compile-time separation between entity id types and reject an id of the wrong type during compilation.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.