Tag: 类型

是否有可能在when语句中返回与type参数相同的类型

例如: fun <T> f(a: T): T = when (a) { a is Int -> 0 // if T is Int, then return Int a is String -> "" // if T is String, then return String else -> throw RuntimeException() // Otherwise, throw an exception so that the return type does not matter. } 它给编译错误: […]

Kotlin实体类型参数不能作为函数体中的类型参数

Kotlin中的特定类型参数可防止类型参数擦除,并允许在运行时知道类型参数。 这允许以下代码编译并按预期方式运行: inline fun <reified T> isA(value: Any) = value is T 但是,当我尝试使用“T”作为类型参数而不是独立时,我得到一个消息,它是一个已擦除类型。 以下代码仅供说明用途 : inline fun <reified T> isListOfA(name: String): Boolean { val candidate = Class.forName(name) return candidate is List<T> } 这是由于技术限制吗? 如果是这样,那么这个限制是什么?

伴侣对象隐藏类 – 错误或功能?

在Kotlin中,以下代码似乎是合理的: data class Foo(val bar: String) { fun combine(other: Foo): Foo { return Foo(bar + other.bar) } companion object Foo { fun someHelper() {} } } 但是,它不会编译:类型Foo绑定到Foo.Foo而不是Foo ! 这是(语言设计或编译器)的错误,还是这是一个功能? 如果是后者,伴侣对象在场的情况下实现的习惯用法是什么? 当然,我会考虑一个解决方法: fun combine(other: my.package.Foo): my.package.Foo { return Foo(bar + other.bar) } 但那不是太好,现在呢?

为什么与私人二传手var是不变的立场?

(我用kotlin 1.1.2-2) 我发现有两种方法可以定义可变的属性,但不能通过=赋值。 var与私人setter val与私有变量后台属性 我也发现他们有不同的行为。 当声明T具有私有setter的T类型的var不能被定义,而具有后备属性的val是合法的。 open class A<out T>(v: T) { // error because T occurs in invariant position var prop1: T = v private set private var _prop: T = v val prop2: T get() = _prop } 为什么prop1是不变的位置, prop2不是? 差异从哪里来?

Kotlin可以在类文件中发出JSR-305注释

我正在使用https://github.com/vojtechhabarta/typescript-generator的 Kotlin来为我在API中使用的类生成TypeScript定义。 为了保留可空性信息,typescript-generator可以使用字段和方法中的注释。 它支持构建脚本中列出的任意注释类名称。 由于某种原因,Kotlin使用@org.jetbrains.annotations.Nullable而不是JSR-305注释@javax.annotation.Nullable注释可空字段。 虽然JSR-305注释具有RetentionPolicy.RUNTIME ,但Jetbrains注释具有RetentionPolicy.CLASS ,使其不适合通过使用Java Reflection来检测注释的库进行反省。 是否有可能让Kotlin生成基于标准的注释而不是JetBrains专有的注释? 如果没有,我对处理这个问题的简单解决方案感兴趣。 也许是一个Gradle插件,可以从JetBrains或类似的产生JSR-305注释。 我真的不希望双重注释一切? 和@Nullable 。

访问外部类型的类型参数

在Kotlin中,我们可以定义嵌套的接口。 如果外部接口是通用的(这里只是为了模拟一个适当的Self元类型,但可以想象其他用途),我可以从内部类型引用它的类型参数吗? 也就是说,我想达到(相当于)这个: interface JsonRepresentable<Self: JsonRepresentable<Self>> { fun toJson(): String interface Companion { operator fun invoke(json:String): Self? } }

如何创建一个可以在Kotlin中使用字符串和函数的变量?

有没有办法创建一个变量来存储字符串和函数? 像var x:dynamic其中x可以是任何类型或函数: x="foo"; x= {print (…)} x="foo"; x= {print (…)} dynamic不是一种类型(它只是关闭类型检查),只能在kotlin.js(JavaScript)中使用。 是否有包含函数类型和Any的类型?

Kotlin:“val someVar = if(xx)1 else 1.0”,为什么someVar是“Any”?

首先,我试过: interface Super class A : Super class B : Super val a = if (System.currentTimeMillis() >= 100) A() else B() 我按Ctrl Q来检查一个类型。 Super预期的。 但是当我尝试: val someVar = if (System.currentTimeMillis() > 0) 1 else 1.0 它说一些someVar是一个Any 。 它不应该是一个Number吗?

Kotlin:与lambda和泛型混淆

请在评论中看到错误信息: interface Printable {} class Book(val title: String) :Printable fun bookPrint(b: Book?):String = "Title: " + b?.title class Author(val name: String) :Printable fun authorPrint(a: Author?):String = "Name: " + a?.name // Unsupported: [modifier on parameter in function type] // ————-vv fun printIt(f: (in Printable?) -> String, a:Printable):String { return "Unknown: " + f.invoke(null) + "Known: […]

Smartcast是不可能的,因为propery有开放或自定义的getter

我正在学习Kotlin。 我的代码如下: override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) decoupler.attachNotifier(this) if(activity is ScreenRouter) { decoupler.attachRouter(activity) } } attachRouter()方法: fun attachRouter(router: ScreenRouter?) { this.router = router } 正如在文档中所写的,kotlin在与操作员核对之后自动进行类型转换。 所以,我预计它会工作。 但相反,它困扰我编译错误说: Smartcast到ScreenRouter是不可能的,因为activity是一个开放的或自定义的getter。 我想也许错误是因为活动可以为空,所以我试过: if(activity!=null && activity is ScreenRouter) { decoupler.attachRouter(activity) } 但它没有工作,编译失败,同样的错误。 但是,下面的代码工作正常: if(activity is ScreenRouter) { decoupler.attachRouter(activity as ScreenRouter) } 它没关系,但上面的错误似乎并没有解释为什么smartcast失败的任何事情。 我不是Kotlin的专家,我只是一个学习Kotlin的初学者。 我没有找到任何文件。 这些错误的描述使Kotlin可怕的学习。 […]