Is there a way to initialize a delegated property ...
# getting-started
y
Is there a way to initialize a delegated property in the same line? For instance:
Copy code
fun main() {
    val test by MyCustomDelegate() = 5 //doesn't work
    test = 5 //This is the current workaround
}
I don't want to change the implementation of MyCustomDelegate per se, I just want to call its setter in line. Is that currently possible?
e
your example doesn't work because
val
can't be assigned 😅
but is this not something you can handle when initializing the delegate itself? e.g. with the standard read-write delegates such as
Copy code
Delegates.observable(initialValue) { ... }
y
Whoops, yeah I intended for it to be a var, and my use case is as such 🤦 I can handle it upon initialization, sure, but that requires changing how the class is defined.
In my case, I'm using a class that wasn't intended to be a delegate as a delegate using extension
getValue
and
setValue
I guess I can handle this in
provideDelegate
, but I was wondering if there's a neater way