为什么当我尝试返回一个检查的generics参数的值时出现types不匹配?

在下面的代码中, “Happy Halloween!” , 42等被标记为“types不匹配”。 (必需:T,find:字符串(或Int)),但不应该编译器能够推断返回值types检查是正确的types? interface Type class StringType() : Type class IntType1() : Type class IntType2(val a: Int, val b: Int) : Type fun something(type: Type): T = when (type) { is StringType -> “Happy Halloween!” is IntType1 -> 42 is IntType2 -> type.a * type.a + type.b * type.b + type.a * […]

如何在Kotlin“预加”一个字符串到字符串

这可能是最新的Kotlin问题:在Kotlin中,我可以在字符串前添加一个字符? 例如 fun main(args: Array) { val char = ‘H’ val string = “ello World” val appendingWorks = string + char //but not what I want… //val prependingFails = char + string //no .plus(str:String) version val prependingWorkaround1 = char.toString() + string val prependingWorkaround2 = “” + char + string val prependingWorkaround3 = String(charArray(char)) + string […]

“重载解析模糊度错误”与另一个重载解决

我看到了这个问题 。 怎么可能是这条线添加了 fun Int.toUpperCase() = “” 这个问题 错误:(6,16) 重载分辨率模糊性: @InlineOnly public inline fun Char.toUpperCase():在kotlin.text中定义的字符 @InlineOnly public inline fun String.toUpperCase():在kotlin.text中定义的字符串 离开这段代码? fun main(args: Array){ var ab: String? = “hello” ab = null println(ab?.toUpperCase()) } 给出的参考问题的答案是有道理的,它只是不回答这里发生的事情。

Kotlin Coroutines文档中的素数示例如何工作?

我正在通过Kotlin的协程文件,并且一直沿用这个例子。 当我发现一个素数时,我很难理解它是如何计算的,特别是如何从filter函数返回并分配给cur ,以及如何从numbersFrom方法生成数字。 我已经添加了调试语句,以尝试并遵循正在运行的各种协程,但是我仍然失去了启动新协程并从其他人接收数字的逻辑流程。 https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#prime-numbers-with-pipeline 代码: fun log(msg: String) = println(“[${Thread.currentThread().name}] $msg”) fun main(args: Array) = runBlocking { var cur = numbersFrom(context, 2) for (i in 1..10) { val prime = cur.receive() println(prime) cur = filter(context, cur, prime) } } fun numbersFrom(context: CoroutineContext, start: Int) = produce(context) { var x = start while (true) { […]

为什么公共课不能扩展私人课程?

我有一个类的层次结构: interface Repository // This class contains some common stuff for LocalRepository and RemoteRepository. I’m never going to use this class outside this file, so I make it private private abstract class BasicRepositoryImpl // these classes are designed to be instantiated in other files class LocalRepository : BasicRepositoryImpl(), Repository // error class RemoteRepository : BasicRepositoryImpl(), […]

通过委托来简化属性获取器

我有很多属性遵循这个模式,基本上只有从下面的模板变化的东西是: 初始化的值 属性名称 码 var foo: Double = 0.0 get() { update() return field } var foo2: Double = 1.23 get() { update() return field } 题 有什么办法可以使用委托来简化(减少代码的冗长)吗?

android数据绑定unit testing错误解析数据绑定编译器选项失败。 PARAMS:

当我尝试为我的kotlin android项目设置unit testing时,遇到以下错误。 以前有没有遇到过这样的问题? 这已被标记为官方错误跟踪器的错误 失败:生成失败,出现exception。 * 什么地方出了错: 执行失败的任务’:app:compileDebugUnitTestJavaWithJavac’。 > java.lang.RuntimeException:无法解析数据绑定编译器选项。 PARAMS: kapt.annotations:/Users/trevor/Dev/Android/workspace/gitlab-example/app/build/tmp/kapt/debugUnitTest/wrappers/annotations.debugUnitTest.txt kapt.kotlin.generated:/ Users / trevor / Dev / Android / workspace / gitlab-example / app / build / tmp / kapt / debugUnitTest / kotlinGenerated *尝试: 运行–info或–debug选项来获得更多的日志输出。 *例外是: org.gradle.api.tasks.TaskExecutionException:对于任务“:app:compileDebugUnitTestJavaWithJavac”,执行失败。 在org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:84) 在org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:55) 在org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62) 在org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58) 在org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88) 在org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:46) 在org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:51) 在org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54) 在org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43) 在org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34) 在org.gradle.execution.taskgraph.DefaultTaskGraphExeter $ $ EventFiringTaskWorker […]

未解决的参考:Kotlin中的findViewById

fun Tryouts() { var CheckBox1 : CheckBox = findViewById(R.id.ForwardBox) as CheckBox CheckBox1.setChecked(false) } 我还是Kotlin的初学者,仅仅学习了kotlin的基本工作,我无法引用任何android小部件或者在Android Studio中更改它的状态,无论是TextView还是CheckBox或者RadioBox。 在所有情况下,findViewById的相同未解决的引用错误… 我不知道我在做什么错误,甚至是java转换输出相同的错误。

检查Kotlin中的字符串是否为空

在Java中,我们总是提醒使用myString.isEmpty()来检查一个字符串是否为空。 但是,在Kotlin中,我发现可以使用myString == “”或myString.isEmpty()甚至myString.isBlank() 。 有没有关于这方面的指导方针/建议? 还是仅仅是“任何事情都会让你的船摇晃”? 提前感谢喂养我的好奇心。 :d

从android操作系统检索国家列表

我正在寻找一种方法来填充一个微调的国家名单与他们的名单。 我可以从Android操作系统检索它吗? 有人可以给我一个例子吗?