why can't I call this function from the code? ```o...
# announcements
s
why can't I call this function from the code?
Copy 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
Well you should be able like this:
Copy code
with (Extensions) {
  mySharedPrefs.getObject<Object>(KEY)
}
But perhaps it would be better to make it top-level function
👍 1
✔️ 2
s
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
Unrelated, but you should avoid creating a new Gson instance every time you fetch something
✔️ 1