Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend @ContributesTo to capture @Provides on top-level functions #98

Open
JvmName opened this issue Jan 3, 2025 · 1 comment
Open

Comments

@JvmName
Copy link

JvmName commented Jan 3, 2025

I think it'd be pretty cool if you didn't need to wrap @Provides inside a @Component.

This doesn't work:

//in file network.kt
@Provides
fun provideOkhttpClient(): OkHttpClient {
    return OkHttpClient.Builder().build()
}

Today, you have to write something like this instead:

@ContributesTo(AppScope::class)
interface NetworkComponent {
  @Provides fun okhttp(): OkHttpClient { ... }
}

What if, instead, you could write this:

@Provides @ContributesTo(AppScope::class) 
fun provideOkhttpClient(): OkHttpClient {
    return OkHttpClient.Builder().build()
}

Or if tracking another annotation via ContributesTo would be annoying, I would even be fine with @ContributesTo(AppScope::class, provides = true) or something similar.

(also linking evant/kotlin-inject#389 (comment), since that was the basis for this)

@vRallev
Copy link
Contributor

vRallev commented Jan 3, 2025

This approach has downsides, e.g. you can replace or exclude contributed types, but you wouldn't be able to reference a contributed function. If you have multiple contributed functions, then you'd repeat the @ContributesTo annotation several times.

You can easily implement such a feature by yourself, though. kotlin-inject-anvil is extensible. Maybe consider adding your own annotation @ProvidesInto(AppScope::class) for top level functions and then you can can generate the needed interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants