title: Kotlin - Cannot use 'T' as reified type parameter. Use a class instead
body: <p>I have a simple helper function to get value from SharedPreferences like this : </p>
  
  <pre><code>operator inline fun &lt;reified T : Any&gt; SharedPreferences.get(key: String, defaultValue: T? = null): T? {
      return when (T::class) {
          String::class -&gt; getString(key, defaultValue as? String) as T?
          Int::class -&gt; getInt(key, defaultValue as? Int ?: -1) as T?
          Boolean::class -&gt; getBoolean(key, defaultValue as? Boolean ?: false) as T?
          Float::class -&gt; getFloat(key, defaultValue as? Float ?: -1f) as T?
          Long::class -&gt; getLong(key, defaultValue as? Long ?: -1) as T?
          else -&gt; throw UnsupportedOperationException("Not yet implemented")
      }
  }
  </code></pre>
  
  <p>I have used reified type parameter to have switching over class type and since it is an operator function, I should be able to call with the square brackets syntax like below :</p>
  
  <pre><code>val name: String? = prefs[Constants.PREF_NAME]
  </code></pre>
  
  <p>But, every time I call it,  UnsupportedOperationException is thrown indicating  function is not able to get class type.</p>
  
  <p>When I attach debugger and evaluate <code>T::class</code>, It is giving me an error <code>"Cannot use 'T' as reified type parameter. Use a class instead."</code></p>
  
  <p>What's wrong with my function? I could not catch the mistake. can anyone help?</p>
  
  <p>Edit : The whole class is <a href="https://gist.github.com/krupalshah/782c42c70f2c58004c9bbda6291315e6" rel="noreferrer">here</a> and <a href="https://gist.github.com/krupalshah/d091be195999a7ddf52c3bb0f04f2bf3" rel="noreferrer">this is where</a> I get the error.</p>
  
  <p><strong>Update:</strong> It seems to be Kotlin compiler issue. 
  Track <a href="https://youtrack.jetbrains.com/issue/KT-17748" rel="noreferrer">https://youtrack.jetbrains.com/issue/KT-17748</a> and
  <a href="https://youtrack.jetbrains.com/issue/KT-17748" rel="noreferrer">https://youtrack.jetbrains.com/issue/KT-17748</a> for updates.</p>