https://kotlinlang.org logo
Title
d

dimsuz

01/09/2019, 4:04 PM
Is there a way to specify that I want to use boxed version of
val
? I use DI framework and it finds primitive
boolean
here:
class MyPresenter @Inject constructor(val field: Boolean)
and fails to generate injector. One way is to use
val field: java.lang.Boolean
, but it feels a bit dirty and generates a warning
h

hudsonb

01/09/2019, 6:29 PM
Wouldn't
val field: Boolean?
work?
k

karelpeeters

01/09/2019, 7:17 PM
import java.lang.Boolean as BBoolean
and then use that.
👍 1
d

Dico

01/10/2019, 2:10 AM
Should Boolean really be used as an injectable? I think you should just wrap that
a

Artem Golovko

01/10/2019, 6:34 AM
If you use spring DI you can use
@Configuration
class AppConfig(
@Value("\${my-app.boolean}") private val bool: Boolean)
Or you can use nullable var
@Configuration
class AppConfif() {
  @Value("\${my-app.boolean}") 
  var: Boolean? = null
}
Maybe in your case you could use nullable Boolean?
class MyPresenter @Inject constructor(val field: Boolean?)
Anyway it should be supported by DI framework. If framework doesn't support constructor injection it may be problem in kotlin with primitives, because in Kotlin all primitives is not null unlike Java
d

dimsuz

01/10/2019, 12:11 PM
Thanks all!