Tag: 代表

Kotlin委托属性实现'provideDelegate'操作符函数,但方法从未调用过

我有一个基本类型DbType这是扩展我的模型是这样的: interface DbType { fun <T : Any> field() = PropertyMapper<T>() } PropertyMapper类是这样实现的: class PropertyMapper<out T : Any> internal constructor() { @Suppress("UNCHECKED_CAST") operator fun getValue(inst: DbType, prop: KProperty<*>): T { return ImplementationDetail.values?.get(prop.name) as T? ?: throw NullPointerException("No such ${prop.name} in type ${inst::class}") } operator fun provideDelegate(inst: DbType, prop: KProperty<*>): ReadOnlyProperty<DbType, T> { TODO() } } […]

Kotlin代表更有趣

如果您了解Google的实验性Android架构组件,您可能会知道MutableLiveData 。 试图使它更有趣使用我来与: class KotlinLiveData<T>(val default: T) { val data = MutableLiveData<T>() operator fun getValue(thisRef: Any?, property: KProperty<*>):T { return data.value ?: default } operator fun setValue(thisRef: Any?, property: KProperty<*>, value:T) { if (Looper.myLooper() == Looper.getMainLooper()) { data.value = value } else { data.postValue(value) } } } 然后我可以: var name : String by KotlinLiveData("not given") […]

Kotlin代表团的表达,而不是固定的参考

假设我有一个非常复杂的规范定义为一个接口: interface Spec { fun sayHello() } 而一个标准的实现: class Impl(private val msg: String) : Spec { override fun sayHello() { println(msg) } } 现在假设我想创建一个实现此规范的类并委托给一个实现,但是确切的委托对象在整个对象的生命周期中都是可变的。 这是一个例子: class Derived(var target: Spec) : Spec by target 上面的例子的问题是构造函数参数target被设置为调用构造函数时的委托对象。 然后代理直接访问,而不是执行属性访问。 (这一点已经通过查看Kotlin生成的字节码得到证实。) 所以,即使在构造类后属性target被修改,委托也不会改变。 任何人都可以提供一种在Kotlin中执行这个委托的方法,而不必写出每一种方法吗? 一个理想的解决方案也允许委派一些像lambda或其他表达式一样的东西,只要在对象的整个生命周期中需要委托,就会被评估和用作委托。

用Java的类委托调用Kotlin对象作为静态方法

这可能有点难以描述,所以我会试着举一个我想要做的具体事例。 假设我们有一个Facade接口和类(在java中),像这样: interface FacadeInterface<T> { void method(String from, String via); } class Facade<T> implements FacadeInterface<T> { private Class<T> mClazz; public Facade(Class<T> clazz) { mClazz = clazz; } @Override public void method(String from, String via) { System.out.println("Method called from " + from + " via " + via); } } 在我的应用程序中,我需要拥有多个单例,它们包含外观的一个实例。 真正的门面有额外的设置/配置参数,但这些在这里是不相关的。 在我开始使用kotlin之前,我将有一个类,它持有一个门面的静态实例(不是一个单例,但在我的情况下,它也有类似的用途),它将代码调用到门面上,如下所示: public class Singleton […]

Kotlin:我如何在Java中使用委托属性?

我知道你不能在Java中使用委托属性语法,也不会像Kotlin那样获得“覆盖”set / get操作符的方便,但是我仍然想使用Java中的现有属性委托。 例如,一个简单的int代理: class IntDelegate { operator fun getValue(thisRef: Any?, property: KProperty<*>) = 0 } 在Kotlin当然我们可以这样使用: val x by IntDelegate() 但是我们怎样才能在Java中以某种形式使用IntDelegate呢? 我相信这是开始 final IntDelegate x = new IntDelegate(); 然后直接使用函数。 但是我怎样才能使用getValue函数呢? 我通过什么参数? 我如何获得Java领域的KProperty ?

kotlin在委托中只有一个继承的方法的未解决的参考

我有一个通过方法提供一些代表的科林类。 编译一切时,我得到错误 错误:(121,30)Kotlin:未解析的引用:stringSet 请注意,所有其他方法工作得很好,它编译每一个这种方法的发生 以下是提供代表的课程片段: //Compiles and can be accessed protected fun int(default: Int? = null, jsonName: String? = null, jsonObject: JsonObject = configJson) = JsonProperty({it?.asInt}, jsonName, jsonObject, default.toStringOrNull()) //compiles, but can't be accessed in one class protected fun stringSet(jsonName: String? = null, jsonObject: JsonObject = configJson, default: Set<String>? = null): JsonProperty<MutableSet<String>> { return JsonProperty<MutableSet<String>>({ […]

试图了解Kotlin示例

我想学习Kotlin,并通过try.kotlinlang.org上的示例进行工作 我无法理解一些示例,特别是Lazy属性示例: https : //try.kotlinlang.org/#/Examples/Delegated%20properties/Lazy%20property/Lazy%20property.kt /** * Delegates.lazy() is a function that returns a delegate that implements a lazy property: * the first call to get() executes the lambda expression passed to lazy() as an argument * and remembers the result, subsequent calls to get() simply return the remembered result. * If you want thread […]

kotlin别名属性代表抛出异常

我想将别名属性添加到kotlin中的一些类,这些类只是将自己委托给一些已有的属性 那么我发明了一个名为别名的方法来做到这一点,不幸的是会导致一个异常 val <T> Array<T>.length by alias(Array<T>::size) val <T> Collection<T>.length by alias(Collection<T>::size) fun <R, T> alias(alias: KProperty1<R, T>) = object : ReadOnlyProperty<R, T> { override operator fun getValue(thisRef: R, property: KProperty<*>): T { return alias.get(thisRef) } } 在异常堆栈之后 java.lang.NoSuchMethodError: [Ljava.lang.Object;.getSize()I at kt.ruby.ArrayKt$length$2.get(Array.kt:34) at kt.ruby.ArrayKt$alias$1.getValue(Array.kt:40) at kt.ruby.ArrayKt.getLength(Array.kt)

访问没有实例的Kotlin委托类型

我已经阅读Kotlin中访问属性委托,这是关于从实例访问委托。 可以从Kotlin 1.1开始使用KProperty::getDelegate ,但是这将返回委托的实例,因此需要先创建一个类的实例。 现在我想获得委托的类型,而没有类的实例。 考虑一个具有自定义委托类型CustomDelegate的库,它需要获取委托给CustomDelegate实例的类的所有属性: class Example { var nonDelegatedProperty = "I don't care about this property" var delegatedProperty1 by lazy { "I don't care about this too" } var delegatedProperty2 by CustomDelegate("I care about this one") } 我怎么能,因为我有KClass<Example> ,但不是一个例子的Example ,获取委托给CustomDelegate所有属性?

为什么kotlin不允许covariant mutablemap成为委托?

我是Kotlin的新手。 当我学习在地图中存储属性 。 我尝试下面的使用。 class User(val map: MutableMap<String, String>) { val name: String by map } class User(val map: MutableMap<String, in String>) { val name: String by map } class User(val map: MutableMap<String, out String>) { val name: String by map } 前两个都是工作,最后一个失败了。 用out修饰符, getName的字节码就像这样: public final java.lang.String getName(); 0 aload_0 [this] 1 getfield kotl.User.name$delegate […]