https://kotlinlang.org logo
#getting-started
Title
# getting-started
y

Youssef Shoaib [MOD]

02/19/2022, 1:59 PM
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

ephemient

02/19/2022, 2:46 PM
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

Youssef Shoaib [MOD]

02/19/2022, 2:54 PM
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
4 Views