https://kotlinlang.org logo
Title
j

Javier

04/06/2019, 12:07 PM
Is there any way to check if a variable is val/var?
e

earroyoron

04/06/2019, 12:43 PM
For what? I dont imagine a use case for that
j

Javier

04/06/2019, 12:57 PM
interactive course
s

streetsofboston

04/06/2019, 1:39 PM
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

Paul Woitaschek

04/06/2019, 2:07 PM
I assume the metadata annotation contains info on that
j

Javier

04/06/2019, 3:44 PM
@streetsofboston I tried yesterday with KProperty and KMutableProperty but I didn't get it working
s

streetsofboston

04/06/2019, 4:00 PM
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

Javier

04/06/2019, 4:23 PM
@streetsofboston I tried
is KMutableProperty
but it says KProperty is incompatible with String
s

streetsofboston

04/06/2019, 4:31 PM
String
? Did you try something like this:
loadedEmployee::firstName is KMutableProperty<*>
(note the use of
::
instead of
.
)
j

Javier

04/06/2019, 4:34 PM
it works, thank you!