Overview
once-cell-kotlin provides single-assignment cells and lazy initialization primitives for Kotlin Multiplatform across JVM, Android, Apple (iOS, macOS, tvOS, watchOS with native Swift 6 Export), Linux, Windows, JavaScript, and WebAssembly.
Unlike a mutex or read-write lock, a OnceCell can be written to at most once. Because an initialized value is never overwritten through a shared reference, get() returns a direct reference (T?) without lock guards, wrapper proxies, or contention after initialization.
Three Specialized Modules
Installation
Gradle (Kotlin DSL)
kotlin {
sourceSets {
commonMain.dependencies {
implementation("io.github.kotlinmania:once-cell-kotlin:0.1.1")
}
}
}
Gradle Version Catalog (gradle/libs.versions.toml)
[versions]
once-cell = "0.1.1"
[libraries]
once-cell-kotlin = { module = "io.github.kotlinmania:once-cell-kotlin", version.ref = "once-cell" }
Quick Start
1. Thread-Safe sync.OnceCell
sync.OnceCell<T> allows safe shared initialization across threads. If an initializer throws, the exception propagates to the caller and the cell remains uninitialized so a subsequent call can retry.
2. Thread-Safe sync.Lazy
sync.Lazy<T> combines a OnceCell<T> with an initializer lambda, evaluating on first access via .value or Lazy.force(lazy):
import io.github.kotlinmania.oncecell.sync.Lazy
val globalData: Lazy<Map<Int, String>> = Lazy.new {
buildMap {
put(13, "Spica")
put(74, "Hoyten")
}
}
fun main() {
println(globalData.value)
}
3. Blocking Wait Across Threads (sync.OnceCell.wait)
When one thread is responsible for setting a cell and other threads need the result, wait() blocks until initialization completes:
import io.github.kotlinmania.oncecell.sync.OnceCell
val cell = OnceCell.new<Int>()
cell.set(92)
val answer: Int = cell.wait()
check(answer == 92)
4. Wait-Free Atomic Cells (race)
The race module provides wait-free cells built on single atomic compare-and-set operations. No thread ever blocks waiting on another thread:
import io.github.kotlinmania.oncecell.race.OnceBool
import io.github.kotlinmania.oncecell.race.OnceBox
import io.github.kotlinmania.oncecell.race.OnceNonZeroUsize
val idCell = OnceNonZeroUsize.new()
val id: ULong = idCell.getOrInit { 42uL }
check(id == 42uL)
val flag = OnceBool.new()
check(flag.getOrInit { true })
val boxCell = OnceBox.new<String>()
greeting = boxCell.getOrInit { }
check(greeting == )
5. Single-Threaded Cells (unsync)
When thread synchronization is unnecessary, unsync.OnceCell and unsync.Lazy eliminate atomic operations altogether and immediately detect illegal reentrant initialization:
import io.github.kotlinmania.oncecell.unsync.Lazy
import io.github.kotlinmania.oncecell.unsync.OnceCell
val cell = OnceCell.new<String>()
val value = cell.getOrInit { "initialized" }
check(value == "initialized")
val sum = Lazy.new { listOf(1, 2, 3).sum() }
check(sum.value == 6)
6. Swift 6 Export (OnceCellLibrary)
once-cell-kotlin ships first-class Kotlin Swift Export support for Apple platforms. In addition to generic Kotlin classes, strongly-typed facades (StringOnceCell, IntOnceCell, StringLazy, IntLazy) and SAM fun interface initializers bridge cleanly into Swift 6 without erasure:
import OnceCell
let syncCell = sync.StringOnceCell()
_ = syncCell.set(newValue: "hello from Swift")
assert(syncCell.get() == "hello from Swift")
let flag = race...shared.new()
flag.set(value: )
(flag.get() )
Multiplatform Target Matrix
Every commit and release of once-cell-kotlin is built and verified across the full KotlinMania target surface with allWarningsAsErrors = true:
Building & Testing Locally
./gradlew test
./gradlew build
Acknowledgments & License
Special Thank You
A heartfelt thank you to Aleksey Kladov (@matklad) and all contributors to the original Rust once_cell crate. Their elegant API design, lock-free state machine architecture, and comprehensive test suite inspired both Rust's standard library (std::cell::OnceCell / std::sync::OnceLock) and this Kotlin Multiplatform transliteration.
License
once-cell-kotlin is dual-licensed under the same terms as the upstream project, at your option:
- MIT License — see
LICENSE-MIT or LICENSE
- Apache License, Version 2.0 — see
LICENSE-APACHE
Original Rust once_cell work: Copyright (c) 2018 Aleksey Kladov and once_cell contributors (matklad/once_cell).
Kotlin Multiplatform port: Copyright (c) 2026 Sydney Renee and The Solace Project (KotlinMania/once-cell-kotlin).