Tag: 仿制药

Kotlin通用范围为Class

以下generics不能编译。 这里的语义错误是什么? 函数调用 start(MainActivity.javaClass) // <== Doesn't compile 方法定义 // Definition public fun start(activityClass: Class) { startActivity(Intent(this, activityClass)) } 编译器错误 Error:(43, 9) Type parameter bound for T in fun start(activityClass: java.lang.Class): kotlin.Unit is not satisfied: inferred type com.mobsandgeeks.hellokotlin.MainActivity. is not a subtype of android.app.Activity

Kotlingenericstypes参数

在以下源代码 fun main(args: Array) { println(“Hello, world!”) val mutableIntList = mutableListOf(1, 2, 3) addInt(4, mutableIntList) // No compile-time error addAnotherInt(5, mutableIntList) // Compile-time error println(mutableIntList) } fun addInt(item:T, list:MutableList){ list.add(item) } fun addAnotherInt(item:T, list:MutableList){ list.add(item) } 函数addInt和addAnotherInt作为参数是一个逆MutableList的Number MutableList 。 但是在main函数中,一行通常编译,另一行不行。 我也检查了从这些函数生成的Java代码,他们似乎是相同的。 addInt和addAnotherInt函数有addInt addAnotherInt ?

Kotlingenericstypes的问题

我在几个小时内就被遗忘了。 我想简化代码,但是当它的genericstypes不能投射对象时。 我怎样才能解决这个问题或另一种简化的方式? (I’m use kotlin 1.2) sealed class SETTING(override val key: String) : SettingsKeyContract object optFirstLaunch : SETTING(“first_launch”), SettingsKeyContractWithType { override val defaults = true } object optNotifications : SETTING(“notification_list”), SettingsKeyContractWithType<List> { override val defaults = emptyList() } interface SettingsKeyContract { val key: String } interface SettingsKeyContractWithType : SettingsKeyContract { val defaults: T @Suppress(“UNCHECKED_CAST”) […]

如何用原始types在kotlin中调用函数

当一个函数声明一个types参数时: fun typedFunction(value: T, option: Option) { … } 我应该如何在kotlin中调用raw un-typed typedFunction? 为什么? 在Java中我有: // This is a method in an external library. I can not change it. void typedFunction(T t, Option o) { … } // This is my code. optionsValues contains many types // such as Option, Option, and … Map<Option, ?> m […]

了解Kotlin中的物化和通配符如何工作

如果问题的题目不是很重要,我很抱歉,但是我还没有find更好的方法来描述这个问题。 我正在为Hadoop编写一些Kotlin扩展方法,昨天我列出了一个我不明白的奇怪错误。 我写了几个这样的扩展方法: inline fun <reified T : InputFormat, reified K : Mapper> Job.addMultipleInputPath(path: Path) { MultipleInputs.addInputPath(this, path, T::class.java, K::class.java) } inline fun <reified T : OutputFormat, reified Key : Any, reified Value : Any> Job.addMultipleNamedOutput(namedOutput: String) { MultipleOutputs.addNamedOutput(this, namedOutput, T::class.java, Key::class.java, Value::class.java) } inline fun <reified T : Mapper, reified KeyClass : Any, reified […]

通用扩展类和实现Kotlin中的接口

说我想要一个typesvariables,T,扩展一个类,并实现一个接口。 就像是: class Foo { … } 这在Kotlin中的语法是什么?

Kotlingenerics改变返回types

我在Java中有这种方法我想转换为Kotlin,但仍然使用Java和Kotlin: @Nullable public static T nullIfEmpty(@Nullable final T t) { return TextUtils.isEmpty(t) ? null : t; } 在另一个地方,我有一个接受字符串的方法: public static doSomething(@Nullable String foo) {} 我这样叫: String foo = “”; doSomething(nullIfEmpty(foo)); // == doSomething(null); since foo is empty 在将nullIfEmpty()转换为这个Kotlin代码片段之后: fun nullIfEmpty(t: T?): T? { return if (TextUtils.isEmpty(t)) null else t } Java不再编译抱怨有一个参数types不匹配 – 当它期望一个String时用CharSequence调用该函数。 什么是正确的语法来解决这个问题?