why `bylazy` doesn't work with `var` ? I know we h...
# getting-started
g
why
bylazy
doesn't work with
var
? I know we have
lateinit
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
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
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
That's not the issue. This is how delegates function. A var-supporting Lazy would simply need to implement
setValue
and that's it