toothpick icon indicating copy to clipboard operation
toothpick copied to clipboard

Boolean kotlin injection

Open emartynov opened this issue 4 years ago • 1 comments

So I spend some time today trying to inject boolean field.

I have class A @Inject constructor(@Named("name") val booleanValue: Boolean)

I created module:

internal class BooleanModule(booleanValue: Boolean) : Module() {
  init {
    bind(Boolean::class.java).withName("name").toInstance(booleanValue)
  }

Iteration 1:

  • Code generation fails since Boolean transformed to primitive by kotlin compiler and Toothpick don't have such functionality

Iteration 2:

  • I forced kotlin to keep Boolean class class A @Inject constructor(@Named("name") val booleanValue: Boolean?) This compiles but fails in the runtime since it can not find the provider No binding was defined for class java.lang.Boolean and name

Iteration 3:

  • I changed it to Any in the class and just cast it in runtime.

Am I doing something wrong and there is the simple solution?

emartynov avatar Jan 14 '21 16:01 emartynov

I decided to go with the additional class InjectableBoolean that is just wrapper around boolean.

emartynov avatar Jan 15 '21 09:01 emartynov