title: How to get the name of a variable in Kotlin?
body: <p>I have a Kotlin class in my application with a lot of attributes, what I want to build is a method that stores the variable name in a dictionary. The dictionary looks like this: </p>
  
  <pre><code>HashMap&lt;String, Pair&lt;Any, Any&gt;&gt;()
  </code></pre>
  
  <p>The purpose of this is to store the changes made to a certain attribute, I store the name of the variable as the key and in the Pair I store the old value and the new value. To notify a change I use the Observer pattern. So whenever a setter is called from an attribute a change will be notified and stored to the dictionary. </p>
  
  <p>The code below results in the folowing: </p>
  
  <pre><code>var person = Person("Harry", 44)
  person.age = 45
  </code></pre>
  
  <p><code>HashMap("age", (44, 45))</code></p>
  
  <p>Right now I am just hardcoding the variable name in as a String, so my question is: </p>
  
  <p><strong>How to dynamicly get the name of a variable in Kotlin?</strong></p>
  
  <p>I saw the same question in Java: <a href="https://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable">Java Reflection: How to get the name of a variable?</a></p>
  
  <p>Also some other questions about the same topic claiming it is not possible: <a href="https://stackoverflow.com/questions/18674153/get-the-name-property-of-a-variable">Get the name property of a variable</a></p>
  
  <p>I can understand that it is not possible to get the name of a variable, because the compiler simple doesn't have that information, but I am still currious to see if others have any sollution for this problem. </p>