Xad Kile
11/17/2022, 3:35 AMval myVar by lazy{ something() }
, but does not cache the value, but re-evaluates the value every time myVar
it is access? myVar
is a local variableephemient
11/17/2022, 3:46 AMval myVar
get() = something()
Xad Kile
11/17/2022, 3:51 AMmyVar
is a local variable. Is there a built-in for that? Thank youephemient
11/17/2022, 4:00 AMval myVar by ReadOnlyProperty { _, _ -> something() }
ephemient
11/17/2022, 4:02 AMoperator fun <T> (() -> T).getValue(thisRef: Any?, property: KProperty<*>): T = invoke()
which would allow you to write
val myVar by { something() }
gildor
11/17/2022, 4:37 AMsomething()
I feel that it shows intent and behaviour better and would cause unexpected results like:
if (myVar == anotherVar || myVar == oneMoreVar)Xad Kile
11/17/2022, 4:52 AMlateinit var var1
. On JUnit I would just use get()
to make shortcut to `var1`'s properties, but since this is kotest, everything is stored inside a lambda and get()
can't be applied to local variables, and I don't want to repeat var1.p1
. It looks something like this:
KotestLambda {
lateinit var var1:Any
val p1 by myDelegate{var1.p1}
beforeTest{
var1 = initVar1()
}
test("test 1"){
// p1 is accessed multiple times.
}
}
gildor
11/17/2022, 4:53 AMI don’t want to repeatPersonal opinion, I would rather repeat it instead of introducing additional property for this Ehh, this lateinit is really nasty 🙈var1.p1
// p1 is accessed multiple times.Can you just declare local p1 on
test 1
level?Xad Kile
11/17/2022, 4:58 AMlateinit
nasty? I would like to know more.
Of course I can declare it in test 1, but it is also used in many other tests. So, after typing this out for like 20 times, I thought I should make a shortcut.gildor
11/17/2022, 5:08 AMgildor
11/17/2022, 5:09 AMXad Kile
11/17/2022, 5:12 AMgildor
11/17/2022, 5:16 AMXad Kile
11/17/2022, 5:18 AMgildor
11/17/2022, 5:19 AMtest("test 1") {
val p1 = initVar1().p1
}
Xad Kile
11/17/2022, 5:22 AMephemient
11/17/2022, 5:24 AMclass MyActivity @Inject constructor(...)
etc. on Android 9+, and somebody sufficient motivated could use AsmClassVisitorFactory
to bring that to older APIs. but that's pretty far astray from this topic :)gildor
11/17/2022, 5:26 AM