Kotlin DDD Phantom type

🇺🇦 Eugen Martynov
2 min readOct 22, 2023

I was on twitter and got suggestion to read post-introduction about phantom type by Vincent Pradeilles. The idea is a code safety improvement with a minimum boilerplate and performance penalty.

The Kotlin equivalent would be next. Let’s pretend that you have code:

data class User(val id: String)
data class Address(val id: String)

val me = User(id = "test id")
val home = Address(id = "test id")

if (me.id == home.id) {
assert("Wrong! Person can not be address")
}

Isuru Rajapakse came with the next improvement:

@JvmInline value class UserId(val value: String)
@JvmInline value class AddressId(val value: String)

data class User(val id: UserId)
data class Address(val id: AddressId)

val me = User(id = UserId("test id"))
val home = Address(id = AddressId("test id"))

if (me.id == home.id) {} // This will not compile

That has minimum performance penalty but still require some boilerplate code writing.

Then Ivan Canet proposed an improvement that will require also minimum boilerplate and will be 100% equivalent to the swift code:

@JvmInline value class Id<out T>(val value: String)

data class User(val id: Id<User>)
data class Address(val id: Id<Address>)

val me = User(id = Id("test id"))
val home = Address(id = Id("test id"))

if (me.id == home.id) {} // This will not compile also

That is neat!

This code works only with Kotlin JVM backend (JVM and Android) and Kotlin version 1.7.20+

Read more about:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

🇺🇦 Eugen Martynov
🇺🇦 Eugen Martynov

Written by 🇺🇦 Eugen Martynov

Stand with Ukraine! The loving XP husband and freelance Android/Kotlin engineer.

Responses (2)

Write a response

if (me.id == home.id) {} // This will not compile also

Noticed that this code compiles for me. I use Kotlin 1.9.23
UPD: My bad. I've missed `out` before generic 🤦 It works perfectly!

1

Still don't understand the benefits of this

Recommended from Medium

Lists

See more recommendations