Is there any way to get kotlin property's default ...
# announcements
b
Is there any way to get kotlin property's default value?
d
I'm not sure if Kotlin does defaults.
b
i mean
Copy code
data class Test(val x:String="I'm default")
How do I get "I'm default" from KProperty1<Test,String> ?
d
Ah, this is reflection territory. I'm not sure how but you can get it via reflection.
Can I ask why you need this?
Ah!
x
property does not have a default value!
But
x
constructor parameter has a default value.
b
I need it in JS to handle undefined states...
Ah, got it
That helps
r
Reflection is JS is not possible generally
k
The problem is that default values are actually full pieces of code, for example:
Copy code
fun foo(x: List<Int> = listOf(1, 2, 3)) {}
You can't really get a default value from that, it's an expression that needs to be evaluated every time.
m
Even worse, you can have this:
Copy code
fun foo(x: Int, y: Int = x+1)
k
That's good to know actually, and yes that's a lot worse!