{ if (default is String) return getString(key, default) as T if (default is Int) return getInt(key, default) as T if (default is Float) return getFloat(key, default) as T if (default is Long) return getLong(key, default) as T if (default is Boolean) return getBoolean(key, default) as T throw IllegalStateException("failed to get value") } //MainActivity.kt val sp = getPreferences(Context.MODE_PRIVATE) val a= sp.get("key", 0L) // return as Long val b = sp.get("key", "my default") // return as String