Kotlin Context Parameters: The Feature You’ve Dreamed About
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
Kotlin 2.2 introduces context parameters (Beta), a successor to the experimental context receivers feature. They allow functions to declare multiple implicit dependencies — like Logger, Analytics, or Transaction — without polluting every call site with extra parameters. Unlike the old context receivers, context parameters require explicit receiver names (e.g., logger.log(...) not log(...)), which is a key behavioral difference. The feature is enabled via a compiler flag and is well-suited for cross-cutting concerns like analytics, auth context, feature flags, and transaction scoping. The post covers practical use cases (Flow.stateInWhileSubscribed in KMP, analytics propagation), gotchas (don't use for CoroutineScope inside suspend functions, watch for type ambiguity), and guidance on when to prefer constructor injection instead.
Sort: