Skip to content

Inara has no magic: read the code it writes

  • Kotlin
  • Multiplatform
  • Architecture
  • Deep dive
  • 7 min.

Cmd+click (or Ctrl+click) on any function Inara generated for you, and you land on code you understand at a glance. That's not a happy accident: it's the rule Inara is built on.

Important

Inara always generates concise code that you can understand at a glance. It is always easy to understand how and why it works, which makes debugging a lot easier.

In the announcement of Inara, I wrote that I want to understand what's going on in my code. This article opens the hood on the general cases: a module, its builder, its bindings, a factory. Spoiler: there isn't much to see. That's the point.

But the real proof is in your own project. If you use Inara, I strongly encourage you to read the code it generates for your own modules: Cmd+click is all it takes.

Who owns that database?

Before reading any generated code, let me tell you what I came to dislike in classic DI frameworks such as Kodein DI.

In most of them, every binding goes through its type. Modules are anonymous: they are sets of bindings that have no existence of their own. Once they are loaded, they dissolve into a container, and what's left is a big lookup table where you ask for "a database".

But I don't want a database. I want the database named database, in this module.

That's the difference between a type and an identity. And giving modules an identity is how you build an explicit architecture: you can see which module provides what, and which module depends on which.

In Inara, a module is an interface, so it obviously has a name at compile time. What's less obvious, and what the rest of this article shows, is that it keeps that identity all the way down to the generated code, and to runtime.

What you write, what KSP writes

Let's take the module from the announcement:

NetworkModule.kt
@DIModule
interface NetworkModule {
  val baseUrl: String
  fun httpClient(): HttpClient
}

From this interface, Inara's processor writes a file named InaraNetworkModule.kt. Here is its core:

(Generated)InaraNetworkModule.kt
@InternalInaraGeneration
public class InaraNetworkModule(
  public val baseUrlBinding: Binding<NetworkModule, Unit, String>,
  public val httpClientBinding: Binding<NetworkModule, Unit, HttpClient>,
) : NetworkModule {
  override val baseUrl: String
    get() = this.baseUrlBinding.get(this, Unit)

  override fun httpClient(): HttpClient = this.httpClientBinding.get(this, Unit)
}

That's it: a class that implements your interface, with one field per member.

There is no map keyed by type, and nothing is looked up. The httpClient() of NetworkModule is, quite literally, a field named httpClientBinding. When you call network.httpClient(), you call a method that reads a field.

Notice the this passed to get, too. That's why a binding body sees the module as its receiver, and can read baseUrl when it builds the HttpClient. No clever scoping here: the module simply hands itself over.

The compiler is the graph validator

Next to the class, Inara generates the builder function you call to create a module:

(Generated)InaraNetworkModule.kt
public fun NetworkModule(
  baseUrl: Binding<NetworkModule, Unit, String>,
  httpClient: Binding<NetworkModule, Unit, HttpClient>,
): NetworkModule = InaraNetworkModule(
  baseUrlBinding = baseUrl,
  httpClientBinding = httpClient,
)

One parameter per member, no default values. So what happens if you forget one?

Main.kt
val network = NetworkModule(
  baseUrl = Binding.value("https://kodein-koders.com/wp-json/"),
)
e: file:///…/Main.kt:5:15 No value passed for parameter 'httpClient'.

This is the whole "compile-time dependency check". There is no graph resolution algorithm behind it: it's the Kotlin compiler doing what it always does with a function call. Add a member to the interface, and every builder call that doesn't bind it stops compiling, with an error message any Kotlin developer already understands.

A binding is a tiny object

So what is a binding? Here is the interface from Inara's runtime, without its documentation and its factory functions:

Bindings.kt
public interface Binding<in M, in A, out T> {
  public fun get(module: M, arg: A): T
  public fun copyBinding(): Binding<M, A, T>
}

get produces a value for a module and an argument. Keep copyBinding in mind: we'll get back to it in the next article.

Binding.value returns its value. Binding.provider calls its lambda with the module. And here is how Binding.single gets its value, with the default thread safety:

Bindings.kt
@Volatile private var value: Any? = Uninitialized
private val lock = ReentrantLock()

override fun get(module: M, arg: Unit): T {
  value.ifInitialized<T> { return it }
  return lock.withLock {
    value.ifInitialized<T> { return it }
    builder(module).also { value = it }
  }
}

A @Volatile field and a double-checked lock. You have probably written this exact code yourself at some point.

The entire runtime, on every platform, is about 420 lines of code, comments and blank lines excluded. That's every binding kind, their suspend variants, the annotations and the platform-specific locks. You can read all of it during a coffee break.

Factories: a concession, made readable

I have to be honest here. There is one place where Inara does resolve dependencies by type: @DIFactory.

UsersApi.kt
class UsersApi(val httpClient: HttpClient)

@DIFactory(builds = UsersApi::class, from = NetworkModule::class)
object UsersApiFactory

A factory builds a business class from a module, and assigns each constructor argument from a member of that module, matched by its type. It's a concession, made so that business classes stay completely unaware of Inara.

But this concession comes with two rules I care deeply about.

The first, and the most important: the generated function is easy to read.

(Generated)InaraUsersApiFactory.kt
public fun UsersApiFactory.UsersApi(module: NetworkModule): UsersApi = UsersApi(
  httpClient = module.httpClient(),
)

This is the principle from the introduction at work: Cmd+click (or Ctrl+click) on UsersApiFactory.UsersApi(network), and you see exactly where the dependency comes from: which module, and which member of that module.

The second: resolution happens at compile time. If a dependency is ambiguous, because two members of the same priority match, the code does not compile. If it is missing, it becomes a parameter of the generated function, and the compiler makes sure every caller passes it. Either way, you have to be explicit, for example with an arg function in the factory object:

RepositoryFactory.kt
@DIFactory(builds = Repository::class, from = StorageModule::class)
object RepositoryFactory {
  internal fun StorageModule.argDatabase(): Database = sqlDatabase()
}

In short: either it's obvious and inspectable, or it must be made explicit. There is no third option where a framework silently picks something for you.

The module knows its own name

Remember the identity we talked about? A generated module doesn't lose it at runtime. Print one:

Main.kt
val network = NetworkModule(
  baseUrl = Binding.value("https://kodein-koders.com/wp-json/"),
  httpClient = Binding.single { HttpClient(baseUrl) },
)
println(network)
module NetworkModule {
  val baseUrl: String => value ( String )
  fun httpClient(): HttpClient => single(Synchronized) { HttpClient }
}

The module tells you its name, every member, and how each member is bound. When a test doesn't behave the way you expect, it shows you at a glance which binding each member really holds.

And then there are copy and shadow

Inara also generates two functions to derive a module from another: copy and shadow. They are plain extension functions too, and you can read them in the same file. But they don't behave the same way, and the difference says a lot about how bindings see their module. That's a story for the next article.

No trick, only code

I don't want a DI library that does clever things for me behind my back. I want one whose every line I can read, and whose every dependency I can follow with a click.

That's what Inara is: an interface you write, a class it writes, and a compiler that checks the wiring. Next time you build a module, open build/generated/ksp and read Inara<YourModule>.kt. It should take you about a minute. If it doesn't, that's a bug, and I'd love to hear about it.


Join the conversation:

Inara has no magic: Cmd+click on the code it generates, and you understand it at a glance. A module becomes a class with one field per member. Forget a binding, and the builder call doesn't compile. The whole runtime is about 420 lines. kodein-koders.com/en/blog/inar... #Kotlin #KMP #KSP

[image or embed]

— kodein-koders.bsky.social (@kodein-koders.bsky.social) 6:01 PM · Nov 17, 2026

These articles might interest you

Tutorials, lessons learned and best practices around KOSI and the Kotlin ecosystem, from the people who maintain the libraries.

Opinion
8 min.

09/11/2026

LARPing enthusiasm: the ghost of disengagement

I can hand the work to AI, but not my judgment or my product sense. A story of sliding into disengagement, and of a comeback still in progress.

Read more
Kotlin Multiplatform Architecture Deep dive
6 min.

02/11/2026

Inara: Inject your dependencies at compile time

I created Kodein, yet I now believe it is detrimental to the Kotlin Multiplatform ecosystem. Meet Inara, a Kotlin Symbol Processor that resolves your dependencies at compile time, and learn why we are sunsetting Kodein.

Read more
IA Kotlin Multiplatform Opinion
7 min.

26/10/2026

Agentic coding has killed cross-platform frameworks!

Agents made it cheap to write native code twice, so is cross-platform dead? Not quite: what changed is the question. Share what must be identical, and check it once.

Read more
IA Opinion
6 min.

19/10/2026

LARPing enthusiasm: the AI creativity frenzy

AI lets me build in weeks what once took years, and it feels like a drug. Am I really creating, or just LARPing enthusiasm? A personal reflection on agency, will and shared code.

Read more
Kotlin Multiplatform Deep dive
8 min.

17/10/2026

MocKMP: a mocking processor for Kotlin/Multiplatform

Want to run your unit tests on every Kotlin/Multiplatform target? MocKMP generates mocks and fakes at compile time with a Kotlin Symbol Processor, no reflection needed, and keeps a familiar mocking API.

Read more
Kotlin Multiplatform Deep dive
10 min.

04/09/2025

Use Swift in a Kotlin/Multiplatform library

Need to use a Swift-only iOS API such as CryptoKit in your Kotlin/Multiplatform library? Salomon shows how to bridge it through Objective-C and Kotlin/Native cinterop, with tested code.

Read more