https://kotlinlang.org logo
Title
s

Sergio C.

10/20/2019, 8:49 PM
why can't I call this function from the code?
object Extensions {

    inline fun <reified T : Any> SharedPreferences.getObject(key: String): T? {
        return Gson().fromJson<T>(getString(key, null), T::class.java)
    }
}
can't call mySharedPrefs.getObject<Object>(KEY)
🤔 1
a

Arkadii Ivanov

10/20/2019, 8:53 PM
Well you should be able like this:
with (Extensions) {
  mySharedPrefs.getObject<Object>(KEY)
}
But perhaps it would be better to make it top-level function
✔️ 2
👍 1
s

Sergio C.

10/20/2019, 8:57 PM
Yes that works.
it's strange because this one works fine and can be called
inline fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T {
but getObject fails
I should just make it top level maybe
forget it, I was missing the import getObject at the top
j

John

10/21/2019, 11:26 AM
Unrelated, but you should avoid creating a new Gson instance every time you fetch something
✔️ 1