ghedeon
06/07/2018, 10:14 AMfoo
field is injected via DI, so it has to be lateinit. The thing is, this field is used to build another one — bar
. That's why the bar
is lazy.
@Inject
lateinit var foo
val bar by lazy { foo.foo() }
Now, I'm wondering if I can hide all this trickery behind some delegate or something, so I'll end up with a clean bar
initialization.
Ideal:
val bar by initBar() // somehow foo is injected inside?
Or at least:
@Inject
lateinit var foo
val bar by initBar(foo)
Pavlo Liapota
06/07/2018, 10:30 AMclass Class1 {
lateinit var foo: String
val bar by initBar()
fun initBar() = lazy { foo.toLowerCase() }
}
Or this?
class Class1 {
lateinit var foo: String
val bar by initBar(this)
}
fun initBar(c: Class1) = lazy { c.foo.toLowerCase() }
or this
class Class1 {
lateinit var foo: String
val bar by lazyByFoo()
}
fun Class1.lazyByFoo() = lazy { foo.toLowerCase() }
Why do you want to hide this initialization?ghedeon
06/07/2018, 10:39 AMlazy { longInitMethod(foo) }
, but I was wondering if I can have a delegate that will wrap the whole block, including lazy
arekolek
06/07/2018, 10:43 AMval bar by initBar()
fun initBar() = lazy { foo.foo() }
works for me
You can also do:
lateinit var bar: String
@Inject
fun initBar() {
bar = foo.foo()
}
ghedeon
06/07/2018, 10:46 AMfoo
. I want it to be an utility function, so it could be reused in many places. But then, access to foo
is lost.arekolek
06/07/2018, 10:47 AMfoo
if you could just inject bar
?ghedeon
06/07/2018, 10:50 AMbar
directly, because it's being managed by android framework.. it's bit complicated, but not relevant.ghedeon
06/07/2018, 10:54 AMPavlo Liapota
06/07/2018, 10:54 AMfun main(args: Array<String>) {
val c = Class1()
c.foo = "A"
println(c.bar)
}
class Class1 {
lateinit var foo: String
val bar by initBar { foo }
fun initBar(fooSelector: () -> String) = lazy { fooSelector().toLowerCase() }
}
karelpeeters
06/07/2018, 10:54 AMPavlo Liapota
06/07/2018, 10:55 AMghedeon
06/07/2018, 10:55 AMghedeon
06/07/2018, 10:57 AMfun bind(crossinline foo: () -> Foo) = lazy { foo.invoke().foo() }
karelpeeters
06/07/2018, 10:57 AMghedeon
06/07/2018, 10:59 AMkarelpeeters
06/07/2018, 10:59 AMcrossinline
then? Is that necessary?ghedeon
06/07/2018, 11:00 AMghedeon
06/07/2018, 11:02 AMghedeon
06/07/2018, 11:03 AM@Inject
fun initBar() {
bar = foo.foo()
}
didn't know you can do that. Where the foo is declared? How the DI knows what type to inject?arekolek
06/07/2018, 11:05 AM@Inject lateinit var foo: Foo
there in the same classarekolek
06/07/2018, 11:05 AMinitBar
method right after injecting membersarekolek
06/07/2018, 11:06 AMarekolek
06/07/2018, 11:08 AM@Override
public void injectMembers(SimpleActivity instance) {
SimpleActivity_MembersInjector.injectFoo(instance, fooProvider.get());
injectSetBaz(instance);
}
public static void injectSetBaz(SimpleActivity instance) {
instance.setBaz();
}
Pavlo Liapota
06/07/2018, 11:11 AMlateinit var bar: Bar
@Inject
fun initBar(foo: Foo) {
bar = foo.foo()
}
so foo property is not even need to be declared?arekolek
06/07/2018, 11:15 AMghedeon
06/07/2018, 11:37 AM