简体   繁体   English

Kotlin - 当表达式具有返回类型的函数时

[英]Kotlin - when expression with return type of function

I want to take advantage of kotlin's when expressions and generic methods to simplify shared preferences api of Android. 我希望利用kotlin的表达式和通用方法来简化Android的共享首选项api。

Instead of calling getString() & getInt() etc. all the time, What I want to do is to create an extension function that would switch based on return type of function and call appropriate method. 而不是一直调用getString()和getInt()等,我想要做的是创建一个扩展函数,它将根据函数的返回类型进行切换并调用适当的方法。 Something like below : 如下所示:

  fun <T> SharedPreferences.get(key: String): T? {
        when (T) { //how do I switch on return type and call appropriate function?
            is String -> getString(key, null)
            is Int -> getInt(key, -1)
            is Boolean -> getBoolean(key, false)
            is Float -> getFloat(key, -1f)
            is Long -> getLong(key, -1)
        }
        return null
    }

Of Course, it will not work. 当然,它不会起作用。 But is there any solution to use when expression for return type of a function? 但是当表达式为函数的返回类型时,是否有任何解决方案? All suggestions are welcome. 欢迎所有建议。

To achieve exactly what you want, you can use reified type parameters . 要完全达到您的要求,您可以使用reified类型参数 This will make the compiler inline your function at its call sites with T replaced with the type used at the call site. 这将使编译器在其调用站点内联您的函数,其中T替换为在调用站点使用的类型。

The function would look like: 该功能看起来像:

@Suppress("IMPLICIT_CAST_TO_ANY")
inline operator fun <reified T> SharedPreferences.get(key: String): T? =
    when (T::class) {
        String::class -> getString(key, null)
        Int::class -> getInt(key, -1)
        Boolean::class -> getBoolean(key, false)
        Float::class -> getFloat(key, -1f)
        Long::class -> getLong(key, -1)
        else -> null
    } as T?

If you make get an operator function , you can also call it using the operator syntax: prefs[name] . 如果你get一个operator函数 ,你也可以使用运算符语法调用它: prefs[name]

The calls should, of course, provide enough type information for the compiler to infer T : 当然,调用应该为编译器提供足够的类型信息来推断T

val i: Int? = prefs["i"] // OK, the type information is taken from the declaration
val j: Int = prefs["i"]!! // OK

val x = prefs["x"] // Error, not enough type information
val y = prefs.get<String>("y") // OK, the type will be `String?`

fun f(z: Int) = z
f(prefs["z"]!!) // OK, the type information is taken from the parameter type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM