for that case, but what I am wondering about are the restrictions for
var
with
bylazy
. It would be nice to have a property that gets initialized the first time you access it, and then you can change its value later
y
Youssef Shoaib [MOD]
06/05/2024, 9:34 AM
You could probably define your own. I think
lazy
is especially optimized for single initialisation and is made to be safe to access from many threads
đ 1
r
Ronny Bräunlich
06/05/2024, 10:39 AM
Another reason might be the Java bytecode that results out of
by lazy
. E.g. this class:
Copy code
class MyTestClass {
val foo by lazy { "baz" }
}
results in bytecode that doesn't have a String propery, but one of type `Lazy`:
Copy code
public final class MyTestClass {
@NotNull
private final Lazy foo$delegate;
@NotNull
public final String getFoo() {
Lazy var1 = this.foo$delegate;
Object var3 = null;
return (String)var1.getValue();
}
...
}
That shows that you're not supposed to replace the
Lazy
delegate that is present in the bytecode.
y
Youssef Shoaib [MOD]
06/05/2024, 10:55 AM
That's not the issue. This is how delegates function. A var-supporting Lazy would simply need to implement