If I define a lazy property with an initializer as...
# getting-started
a
If I define a lazy property with an initializer as in this code. Does initializer have access to X and if so how is it accessed?
v
if
initializer
is a lambda it can capture the local closure, so it’d have an implicit
this
which would refer to an instance of
X
a
Would something like: val initializer = fun () { return something; } be interpreted as a lambda function?
I suspect not
a
this is an anonymous function, a lambda looks like
{ ... }
a
maybe val initialzer : () -> X = { }
a
this sounds like an XY-problem, what are you trying to achieve?
v
anonymous function should work fine too, as long as it’s defined in the context of the class
a
my reason for not wanting to specify the lambda in the class is that it's quite a bit of code, but then I guess the lambda could just be something like { t -> initializer(t) }
although I'm not sure t in this case refers to the enclosing type
v
{ initializer(this) }
should work fine for you.
a
I have just enough knowledge to be dangerous 🙂
I will try that, thanks!
v
also you can define initializer as extension function if you want to have an implicit
this
though in both cases you won’t have access to non-public stuff
a
makes sense
Thanks again
🙂 1