Implicits

A Swift library for implicit parameter passing through call stacks. Eliminate parameter drilling and simplify dependency injection with compile-time safety.
Table of Contents
Installation
Add Implicits to your Package.swift:
dependencies: [
.package(url: "https://github.com/yandex/implicits", from: "1.2.0"),
]
Then add the library to your target:
.target(
name: "MyApp",
dependencies: [.product(name: "Implicits", package: "implicits")],
plugins: [.plugin(name: "ImplicitsAnalysisPlugin", package: "implicits")]
)
The ImplicitsAnalysisPlugin performs static analysis at build time to verify that all implicit parameters are properly provided through the call chain.
Claude Code Plugin
If you use Claude Code, install the bundled plugin so Claude generates correct Implicits code — scope lifetime, key selection, closure handling, and static-analysis constraints:
/plugin marketplace add yandex/implicits
/plugin install implicits@implicits
The plugin contents live in .claude-plugin/ and skills/implicits-usage-guide/.
The Problem
Consider a simple shopping scenario where we need to pass payment details through multiple function layers:
func goShopping(wallet: Wallet, card: DiscountCard) {
buyGroceries(wallet: wallet, card: card)
buyClothes(wallet: wallet, card: card)
buyCoffee(wallet: wallet, card: card)
}
func buyGroceries(wallet: Wallet, card: DiscountCard) {
pay(50, wallet: wallet, card: card)
}
func buyClothes(wallet: Wallet, card: DiscountCard) {
pay(200, wallet: wallet, card: card)
}
func buyCoffee(wallet: Wallet, card: DiscountCard) {
pay(5, wallet: wallet, card: card)
}
func pay(_ price: Int, wallet: Wallet, card: DiscountCard) {
wallet.charge(price * (1 - card.discount))
}
This pattern, known as parameter drilling, requires passing the same arguments through every layer of the call stack, even when intermediate functions don't use them directly. In this simple example the savings may seem modest, but imagine dozens of parameters flowing through many layers — the boilerplate adds up quickly.
The Solution
With Implicits, you declare values once and access them anywhere in the call stack:
func goShopping(_ scope: ImplicitScope) {
buyGroceries(scope)
buyClothes(scope)
buyCoffee(scope)
}
func buyGroceries(_ scope: ImplicitScope) { pay(50, scope) }
func buyClothes(_ scope: ImplicitScope) { pay(200, scope) }
func buyCoffee(_ scope: ImplicitScope) { pay(5, scope) }
func pay(_ price: Int, _: ImplicitScope) {
@Implicit() var wallet: Wallet
@Implicit() var card: DiscountCard
wallet.charge(price * (1 - card.discount))
}
// Usage
let scope = ImplicitScope()
defer { scope.end() }
@Implicit var wallet = Wallet(balance: 500)
@Implicit var card = DiscountCard(discount: 0.1)
goShopping(scope)
Note: Due to Swift's current limitations, a lightweight ImplicitScope object must be passed through the call stack. However, the actual data (wallet, card) doesn't need to be passed — it's accessed implicitly via @Implicit.
Usage Guide
Implicit arguments behave like local variables that are accessible throughout the call stack. They follow standard Swift scoping rules and lifetime management.
Understanding Scopes
Just like regular Swift variables have their lifetime controlled by lexical scope:
do {
let a = 1
do {
let a = "foo" // shadows outer 'a'
let b = 2
}
// 'a' is back to being an integer
// 'b' is out of scope
}
Implicit variables follow the same pattern, but their scope is managed by ImplicitScope objects. Always use defer to guarantee proper cleanup:
func appDidFinishLaunching() {
let scope = ImplicitScope()
defer { scope.end() }
// Declare dependencies as implicit
@Implicit
var network = NetworkService()
@Implicit
var database = DatabaseService()
// Components can now access these dependencies
@Implicit
let search = SearchComponent(scope)
@Implicit
let feed = FeedComponent(scope)