How do I get the value of a `const val` property?
# reflect
r
How do I get the value of a
const val
property?
d
Should be the same as a regular
val
.
r
Nope, it crashes with
Copy code
java.lang.IllegalArgumentException: Callable expects 0 arguments, but 1 were provided.
	at kotlin.reflect.jvm.internal.calls.Caller$DefaultImpls.checkArguments(Caller.kt:20)
	at kotlin.reflect.jvm.internal.calls.CallerImpl.checkArguments(CallerImpl.kt:15)
	at kotlin.reflect.jvm.internal.calls.CallerImpl$FieldGetter.call(CallerImpl.kt:134)
	at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:106)
	at kotlin.reflect.jvm.internal.KProperty1Impl.get(KProperty1Impl.kt:35)
It looks like the
KProperty1Impl.get
method both requires an argument and crashes when one is provided... Somehow
Everything works if I remove
const
, so I'm not blocked, but my question still stands.
d
I am not sure how you got a
KProperty1
for a
const val
.
const val
can only be top level (
KProperty0
) or in an object (also
KProperty0
).
r
I'm iterating over
declaredMemberProperties
which is a
Collection<KProperty1<T, *>>
. It's an object containing only `const val`s
Maybe my `const val`s aren't even in this collection
d
Okay, that is definitely a bug.
r
Tried to log
declaredMemberProperties
, they are in there
d
Workaround:
Copy code
val value = if (prop.isConst) {
    prop.javaField!!.get(null)
} else {
    prop.get(<receiver>)
}
But you should definitely file an issue on YouTrack
Damnit @diesieben07 I was writing my answer 😛
💥 1
d
Derp, sorry
I thought that was a new issue made by you
r
I always look for existing issues before submitting new ones, and there was one. It's kinda old already
I don't think it's something easy to fix anyway, so I'll use your workaround for at least some months