Tag: 仿制药

kotlin列表上的filterNotNull具有泛型类型

这是工作: val values: List<String> = listOf("a", null, "b").filterNotNull() 这不工作: fun <A> nonNullValues(values: List<A?>): List<A> = values.filterNotNull() 编译器抱怨泛型类型: Error:(8, 63) Kotlin: Type parameter bound for T in fun <T : kotlin.Any> kotlin.collections.Iterable<T?>.filterNotNull(): kotlin.collections.List<T> is not satisfied: inferred type A is not a subtype of kotlin.Any 这一个工作: fun <A: Any> nonNullValues(values: List<A?>): List<A> = values.filterNotNull() 有人可以解释我为什么我需要告诉编译器,A是Any的子类型? 我在想每个类型都是Any的子类型… […]

在kotlin中创建一个类型为Comparable <T>的数组的泛型类?

受Kotlin创建通用二维数组的启发,我有下面的代码创建一个类型为T的数组的泛型类。但是,一旦我添加一个上限,我得到一个编译错误。 有没有办法做到这一点? //This code compiles: class GenericClass<T> protected constructor(size : Int, arrayFactory: (Int) -> Array<T>) { companion object { inline fun <reified T> invoke(size : Int) = GenericClass(size, { size -> arrayOfNulls<T>(size) }) } val array = arrayFactory(size) } //Compile errors: class GenericClass<T : Comparator<T>> protected constructor(size : Int, arrayFactory: (Int) -> Array<T>) { companion […]

Kotlin:与泛型混淆

领域返回FooRealm的列表。 FooRealm是一个Foo public class FooRealm extends RealmObject implements Foo 但是我得到一个类型不匹配: Type mismatch: Required: Observable<List<FooRealm>> Found: Observable<List<Foo>> 当我这样做时: override fun getFoo(): Observable<List<Foo>> { return Realm.getDefaultInstance().use { realm -> realm.where(FooRealm::class.java) .equalTo("bar", true) .findAllAsync() .asObservable() } }

KFunction1中的kotlin泛型

假设你有两个类TestA和TestB。 假设TestA扩展了TestB: public class TestB { private int intProp; public int getIntProp() { return intProp; } public void setIntProp(int intProp) { this.intProp = intProp; } } public class TestA extends TestB { private String strProp; public String getStrProp() { return strProp; } public void setStrProp(String strProp) { this.strProp = strProp; } } 现在我创建下一行代码: var getter1: […]

Kotlin“out”和“in”以及泛型 – 正确的用法

我试图做一个通用的穷人的数据持久化功能,将采取MutableSet的数据类和序列化到磁盘。 我想要一个简单的原型设计,并且可以每隔一段时间在设备上调用“save()”,这样如果我的进程被终止了,我可以稍后恢复保存条目的“load()”。 但是,即使在重新读取泛型页面之后,我也不太清楚'*','in','out'和'Nothing'之间的区别。 这SEEMS的工作没有抛出错误,但我不明白为什么他们都“出”,我认为必须是“在”…或者更可能我是理解Kotlin泛型完全错误。 有没有这样做的正确方法? /** Save/load any MutableSet<Serializable> */ fun MutableSet<out Serializable>.save(fileName:String="persist_${javaClass.simpleName}.ser") { val tmpFile = File.createTempFile(fileName, ".tmp") ObjectOutputStream(GZIPOutputStream(FileOutputStream(tmpFile))).use { println("Persisting collection with ${this.size} entries.") it.writeObject(this) } Files.move(Paths.get(tmpFile), Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING) } fun MutableSet<out Serializable>.load(fileName:String="persist_${javaClass.simpleName}.ser") { if (File(fileName).canRead()) { ObjectInputStream(GZIPInputStream(FileInputStream(fileName))).use { val loaded = it.readObject() as Collection<Nothing> println("Loading collection with ${loaded.size} entries.") this.addAll(loaded) } } […]

Kotlin:为了Comparable而去掉Unchecked cast

我刚刚实施了选择排序来熟悉Kotlin,并碰上了一个我觉得难以摆脱的不受控制的演员。 该实现基于kotlin.Comparable接口: object SelectionSort { fun <T> sort(a: Array<Comparable<T>>) { val N = a.size for(i in 0..N – 2) { var min = i for(j in i + 1..N – 1) { if(less(a[j], a[min])) { min = j } } exchange(a, i, min) } } private fun <T> exchange(a: Array<Comparable<T>>, i: Int, min: Int) { […]

Kotlin:麻烦理解泛型

我有一个模型实现两个接口 interface A interface B class Model() : A, B 当我传递一个参数作为我的Model类的List时,编译器知道Model是A和B.但是当我传递两个参数时,其中一个参数的类型为T(其中T:A,T:B),编译器不能理解它。 protected fun <T> test(givenList: List<T>) where T : A, T : B { val testList = ArrayList<Model>() oneParamFunc(testList) // will compile oneParamFunc(givenList) // will compile twoParamFunc(givenList, testList) // won't compile (inferred type Any is not a subtype of A) twoParamFunc<T>(givenList, testList) // won't compile […]

如何在Kotlin的主要构造函数中指定类型参数?

在Java中,我可以这样做: import java.util.List; import java.util.Map; class Foo { public final String s; // Parameters to constructor are generic, but constrained public <K> Foo(List<K> list, Map<K, String> map) { // Compute something from those parameters where result // has type independent of input type parameters. StringBuilder sb = new StringBuilder(); for (K k : list) { […]

如何转换Array <T?>? 到Kotlin的Array <T>中

我正在考虑Kotlin的第一步,并试图编写一个简单的字符串拆分函数。 我从这开始: fun splitCSV(s : String) : Array<String> { return s.split(","); } 我猜也可以这样写: fun splitCSV(s : String) : Array<String> = s.split(","); 但是我得到一个类型错误,因为s.split返回一个Array<String?>? 而不是Array<String> 。 我找不到一个简单的方法来执行转换,所以我编写了这个函数来完成转换: fun forceNotNull<T>(a : Array<T?>?) : Array<T> { return Array<T>(a!!.size, { i -> a!![i]!! }); } fun splitCSV(s : String) : Array<String> = forceNotNull(s.split(",")); 但是,现在我得到一个运行时错误: ClassCastException:[Ljava.lang.Object; 不能转换为[Ljava.lang.String 如果我将forceNotNull T forceNotNull为String,那么它可以工作,所以我想我接近于一个解决方案。 这是正确的方式去呢? […]

Kotlin工厂与泛型的界面

我在我的Kotlin代码中有一个工厂接口,就像这样(使用Guava的TypeToken ): interface ResultMapperFactory { fun <T> get(type: TypeToken<T>, context: MapperLookupContext): ResultMapper<T>? } 到现在为止还挺好。 调用它时非常容易使用,但是实现几乎总是需要不安全的转换: object StringFactory : ResultMapperFactory { override fun <T> get(type: TypeToken<T>, context: MapperLookupContext): ResultMapper<T>? { return if (type.rawType == String::class.java) MyStringMapper as ResultMapper<T> else null } } 这是丑陋的,但我用一个很好的伎俩克服了它。 首先,创建TypeToken实例的一个小实用函数: inline fun <reified T> typeToken(): TypeToken<T> = object : TypeToken<T>() {} 现在我添加了一个伴侣对象到我的工厂界面: […]