Tag: kotlin

单身系列在Kotlin

我想知道是否有可能在Kotlin反序列化(恢复属性值)的声明对象, 而不必手动分配属性或采取反思 。 以下片段进一步解释: object Foo: Serializable { var propOne: String = "" // … fun persist() { serialize(this) // no problem with serialization } fun restore(bytes: ByteArray) { val fooObj: Foo = deserialize(bytes) as Foo // It seems Kotlin allows us to use singleton as type! // obvioulsly either of the following is wrong: […]

将Scala函数转换为Kotlin函数

我对于Scala Kotlin都很陌生,并且试图将一些Scala代码转换成Kotlin,只是为了让我的头脑对事物有所了解。 我遇到的一个问题就是把这个Scala函数转换成Kotlin函数。 def changeXToDigit(value:String): String = { value.map { case 'X' => random.nextInt(10).toString case letter => letter }.mkString } 我知道在Kotlin中没有mkString等价物,但是我想到了类似的东西 fun changeXToDigit(value: String):String = { value.map { it -> when(it) { 'X' -> random.nextInt(10).toString else -> it } } 可能工作,但IntelliJ抱怨,我有点失落的错误。 Error:(11, 45) Kotlin: Inferred type is a function type, but a non-function type String […]

Kotlin配置几个口味

我得到了我的项目配置几个口味的问题。 我有2个env-s,用于分期制作。 它的配置设置为flavor-stage/Env.java和flavor-prod/Env.java ,它的工作原理与Java代码一样,也可以从Kotlin类中看到,但是在编译时会崩溃 Error:(19, 23) Unresolved reference: Env 看来Kotlin可以从另一个风格的文件夹中解析类。 请帮助配置它。 我的gradle.build是: sourceSets { main.java.srcDirs += 'src/main/kotlin' } buildscript { ext.kotlin_version = '1.0.4' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.6" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" } }

不能实现接口kotlin

突然,我的android工作室不允许我实现接口,当我使用Kotlin,我不能导入接口 任何人都遭受与kotlin相同的问题? 问题是真实的,我无能为力解决这个问题 interface AppPrefManager { fun isUserLoggedIn: Boolean }

Kotlin:什么是kotlin.String! 类型

我是Kotlin新手。 你能解释一下“kotlin.String!”是什么意思吗? 类型,我怎么能让下面的代码来编译? fun withDefault<A>(computation: () -> A, default: A) = try { computation() } catch (e: Exception) { default } fun getHostname1() = withDefault(InetAddress.getLocalHost().getCanonicalHostName, "localhost") 编译器打印下面的消息: Kotlin: Type inference failed: fun <A> withDefault(computation: () -> A, default: A): A cannot be applied to (kotlin.String!,kotlin.String) 谢谢。

如何在Kotlin中声明原始类型?

在Java中,我可以声明这一点 private List a; //onCreate a = new ArrayList() 但在Kotlin它显示错误,这迫使我具体的类型 private List<String> a 有时我不想提供一个类型(我不需要),但它显示Kotlin中的错误例如在Java中 public abstract class BaseActivity<T extends ViewDataBinding> extends AppCompatActivity { //something } public abstract class BaseFragment { private BaseActivity activity; //something } //in kotkin I can't write lateinit var activity: BaseAtivity //show error here (I have to specific a type but this […]

访问Kotlin的财产代表

Kotlin已经委派了属性,这是一个非常好的功能。 但有时get()和set()方法是不够的。 假设我想要Closeable地创建一个Closeable对象并稍后关闭它。 下面是一个如何实现这样的委托属性的例子: fun <T : Closeable> closeableLazy(initializer: () -> T) = CloseableLazyVal(initializer) class CloseableLazyVal<T : Closeable>( private val initializer: () -> T ) : ReadOnlyProperty<Any?, T> { private var value: T? = null override fun get(thisRef: Any?, desc: PropertyMetadata): T { if (value == null) { value = initializer() } return value } […]

Kotlin:lambda从不编译

我刚刚开始学习Kotlin,在那里我遇到了lambda语法问题。 有一个小例子: class MathFunctions { @FunctionalInterface interface Operation { fun calculate(a: Int, b: Int): Int } fun makeCalculations(a: Int, b: Int, operation: Operation): Int = operation.calculate(a, b) companion object { @JvmStatic fun main(args: Array<String>) { val plus = Operation { a, b -> a + b } val mathFunctions = MathFunctions() println(mathFunctions.makeCalculations(10, 20, plus)) } […]

Kotlin意想不到的“未解决的参考”

我是Kotlin的初学者,这是我的代码: class C(val boy: Int = 0) { fun <T, E> boy(i: Int) = i } fun girl(b1: Boolean, b2: Boolean) = println("boy($b1, $b2)") fun main(args: Array<String>): Unit { val A = 234 // see? A defined! val B = 345 // see? B defined! val c = C(123) // c is also defined! girl(c.boy […]

编译Kotlin到JavaScript

我已经开始研究Kotlin,我想用它代替TypeScript作为我的前端语言。 都编译到JavaScript,所以我想设置它,使我做一个Kotlin文件,让我们叫它myFile.kt ,然后当我运行编译器,它使一个myFile.js文件。 就像TypeScript如何使用myFile.ts并将其编译到myFile.js 。 我正在使用最新版本的IntelliJ 15与Kotlin的候选版本1。 我一直在搜索整个互联网的方式来做到这一点,但到目前为止,我发现设置IntelliJ,以便Kotlin从您的代码在JAR文件中创建一个JavaScript库。 我也没有能够得到这个编译我的项目中的任何kt文件(他们以前是js文件)。 我现在想做什么,或者我是以错误的方式思考这个问题?