我有这个扩展功能:
inline operator fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T? {
return when (T::class) {
String::class -> getString(key, defaultValue as? String ?: "") as T
Int::class -> getInt(key, defaultValue as? Int ?: -1) as T
Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T
Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T
Long::class -> getLong(key, defaultValue as? Long ?: -1) as T
else -> throw UnsupportedOperationException("Not yet implemented")
}
}
我可以毫无问题地使用它:
但是当我在 Flow 的 builder 函数中使用它时,我收到了这个错误:
没有足够的信息来推断参数 T