Skip to content

Commit

Permalink
Add documentation about support for binding methods in companion obje…
Browse files Browse the repository at this point in the history
…cts.

Issue #4287.

RELNOTES=n/a
PiperOrigin-RevId: 633637694
  • Loading branch information
Chang-Eric authored and Dagger Team committed May 14, 2024
1 parent 9e80b54 commit 55d48d3
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions dev-guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ needs the module at compile time, and can avoid class loading the module at
runtime.
{: .c-callouts__note }

Finally, all `@Provides` methods must belong to a module. These are just classes
that have an [`@Module`][Module] annotation.
Finally, all `@Provides` and `@Binds` methods must belong to a module. These are
just classes that have an [`@Module`][Module] annotation.

```java
@Module
Expand All @@ -152,6 +152,20 @@ interface HeaterModule {
}
```

Note that in Kotlin, `@Provides` methods can also be declared in the companion
object of an `@Module` class.

```kotlin
@Module
interface HeaterModule {
@Binds fun bindHeater(impl: ElectricHeater): Heater

companion object {
@Provides fun provideElectricHeater() = ElectricHeater()
}
}
```

By convention, `@Provides` methods are named with a `provide` prefix, `@Binds`
methods are named with `bind` prefix and module classes are named with a
`Module` suffix.
Expand Down

0 comments on commit 55d48d3

Please sign in to comment.