Is there any way to check if a variable is val/var...
# announcements
j
Is there any way to check if a variable is val/var?
e
For what? I dont imagine a use case for that
j
interactive course
s
No. var/val is just a compiler directive to raise red flag when a 'val' is re-assigned a new value and to possibly do some optimizations. For a 'val' or 'var' property, you may be able to figure it out; you can see if the property is a
KProperty
or
KMutableProperty
(not sure if i have the class-names correc there)
p
I assume the metadata annotation contains info on that
j
@streetsofboston I tried yesterday with KProperty and KMutableProperty but I didn't get it working
s
What did you try? You can either use reflection or obtain a method reference (which is basically a KProperty) to inspect a property's mutability. Then a
is KMutableProperty
should do the trick, or something very similar.
j
@streetsofboston I tried
is KMutableProperty
but it says KProperty is incompatible with String
s
String
? Did you try something like this:
loadedEmployee::firstName is KMutableProperty<*>
(note the use of
::
instead of
.
)
j
it works, thank you!