Quick Answer
Marking experimental API usage explicitly
Understanding the Issue
Kotlin requires explicit opt-in for experimental features. You must annotate your code with @OptIn or @UseExperimental when using APIs marked as experimental.
The Problem
This code demonstrates the issue:
Kotlin
Error
// Problem: Using experimental API
@ExperimentalTime
fun measureTime() {
val duration = TimeSource.Monotonic.measureTime { ... }
}
The Solution
Here's the corrected code:
Kotlin
Fixed
// Solution: Add opt-in annotation
@OptIn(ExperimentalTime::class)
fun measureTime() {
val duration = TimeSource.Monotonic.measureTime { ... }
}
// Or enable for whole file:
@file:OptIn(ExperimentalTime::class)
Key Takeaways
Always explicitly opt-in when using experimental Kotlin APIs.