Kotlin - 如何通过地图使用自定义名称进行属性委托?

Ruc*_*oom 3 delegates properties delegation kotlin

我试图让我的头脑周围的财产代表,我有一个有趣的用例.有可能有这样的事情:

class MyClass {
    val properties = mutableMapOf<String, Any>()
    val fontSize: Any by MapDelegate(properties, "font-size")
}
Run Code Online (Sandbox Code Playgroud)

这将允许我fontSize使用地图作为委托存储,但使用自定义键(即"font-size").

特定用例,用于存储CSS属性标记之类的东西,可以通过变量(fontSize)访问,以便在代码中使用,但在迭代map(font-size: 18px;)时可以正确呈现.

mie*_*sol 8

委托属性的文档是有关该主题的良好信息来源.它可能比以下示例读取时间长一些:

fun <T, TValue> T.map(properties: MutableMap<String, TValue>, key: String): ReadOnlyProperty<T, TValue> {
    return object : ReadOnlyProperty<T, TValue> {
        override fun getValue(thisRef: T, property: KProperty<*>) = properties[key]!!
    }
}

class MyClass {
    val properties = mutableMapOf<String, Any>()
    val fontSize: Any by map(properties, "font-size")
}
Run Code Online (Sandbox Code Playgroud)

您可以稍微简化一下,避免键入CSS属性名称,方法是将Kotlin属性名称转换为CSS属性等价物,如下所示:

fun <T, TValue> map(properties: Map<String, TValue>, naming:(String)->String): ReadOnlyProperty<T, TValue?> {
    return object : ReadOnlyProperty<T, TValue?> {
        override fun getValue(thisRef: T, property: KProperty<*>) = properties[naming(property.name)]
    }
}

object CamelToHyphen : (String)->String {
    override fun invoke(camelCase: String): String {
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, camelCase)
    }
}

fun <T, TValue> T.cssProperties(properties: Map<String,TValue>) = map(properties, CamelToHyphen)

class MyClass {
    val properties = mutableMapOf<String, Any>()
    val fontSize: Any? by cssProperties(properties)
}
Run Code Online (Sandbox Code Playgroud)

上面的例子使用了番石榴CaseFormat.

如果您想拥有可变属性,您的委托必须实现setter方法:

fun <T, TValue> map(properties: MutableMap<String, TValue?>, naming: (String) -> String): ReadWriteProperty<T, TValue?> {
    return object : ReadWriteProperty<T, TValue?> {
        override fun setValue(thisRef: T, property: KProperty<*>, value: TValue?) {
            properties[naming(property.name)] = value
        }

        override fun getValue(thisRef: T, property: KProperty<*>) = properties[naming(property.name)]
    }
}
Run Code Online (Sandbox Code Playgroud)