为什么我不能在typealias上使用@PublishedApi?

在我目前的项目中,我有一个针对不同types查询的公共接口 interface Query 以及该接口的内部实现。 由于我有公共内联函数来创建不同的查询,我需要在@PublishedApi上使用@PublishedApi @PublishedApi internal class QueryImpl : Query 现在为了方便,我想为不同types的查询定义一些types别名。 typealias MatchQuery = Query 这对公共接口非常有效,但我需要在公共内联函数中创建一个实例 inline fun match(init: MatchQuery.() -> Unit) { // It would look nicer if I could use MatchQueryImpl().init() QueryImpl().init() } 但是不必为每种查询都写QueryImpl ,我想为实现使用内部的typealias。 // fails because it needs to be internal typealias MatchQueryImpl = QueryImpl // can’t be used in […]

有没有更好的方法来访问可空属性?

将sound.id属性从可空或不可通过的方式转换为play方法的参数,最好的办法是什么? class Sound() { var id: Int? = null } val sound = Sound() … //smarcat imposible becouse ‘sound.id’ is mutable property that //could have changed by this time if(sound.id != null) soundPool.play(sound.id, 1F, 1F, 1, 0, 1F) //smarcat imposible becouse ‘sound.id’ is mutable property that //could have changed by this time sound.id?.let { soundPool.play(sound.id, […]

Kotlin是否具有身份识别function?

Scala在Predef中有一个通用的identity函数: def identity[A](x: A): A Kotlin在标准库中有类似的function吗? 当然,我可以直接使用{ it } ,但是我发现identity更易于阅读,并且实例化所有这些lambdaexpression式有点浪费。 对于任何types的Foo ,我必须能够在函数(Foo) -> Foo所在的地方使用这个identity函数。 在Kotlin的types系统中,这样的function甚至是可能的吗? (在斯卡拉的情况下,有一个隐式的转换,将函数对象内部的方法封装起来)

输入名称作为值?

有人可以指点我解释以下内容的文件吗? 特别是,我想知道为什么可以使用String和Int ,如图所示。 val a: Unit = { _:Any -> String }(Int) 最初,我写了这个: val a: Unit = { x:Any -> x.toString() }(Unit) 在这两种情况下,我都找不到合适的文档。

它有可能从游艇中断

是否有可能从航线中断。 我的代码: fun test() { bufferedReader.forEachLine { val nameParam = it.split(“:”)[0] if (name == “test”) return // here i wan to return from function } } 我试过’return @ foreachline’,但它只是继续下一行

如何创建Android Facebook密钥哈希?

我根本不理解这个过程。 我已经能够导航到Java SDK中包含keytool的文件夹。 虽然我不断收到错误openssl不被识别为内部或外部命令。 问题是,即使我可以得到这个工作,我会做什么和事后呢?

如何从牙签中的相同模块传递依赖项的实例?

假设我有一个模块,其中一个绑定依赖于另一个: class MyModule : Module(){ init { bind(SettingsStorage::class.java).to(PreferencesBasedSettingsStorage::class.java) // how to use createOkHttpClient here? // how to get instance of SettingsStorage to pass to it? bind(OkHttpClient::class.java).to?(???) } private fun createOkHttpClient(settingsStorage: SettingsStorage): OkHttpClient { return OkHttpClient.Builder() .addNetworkInterceptor(MyInterceptor(settingsStorage)) .build() } } 在这里,我可以创建OkHttpClient只有另一个绑定的实例,即SettingsStorage 。 但是怎么做呢? 目前我看不到在模块内部的SettingsStorage绑定实例将其传递给createOkHttpClient() 在匕首,我会简单地创建两个提供者方法,如适当的参数 fun provideSessionStorage(/*…*/): SessionStorage { /* … */ } fun provideOkHttpclient(sessionStorage: SessionStorage): […]

Java将Image转换成BufferedImage

在StackOverflow上已经有这样的问题了,接受的答案是“cast”: Image image = ImageIO.read(new File(file)); BufferedImage buffered = (BufferedImage) image; 在我的程序中,我尝试: final float FACTOR = 4f; BufferedImage img = ImageIO.read(new File(“graphic.png”)); int scaleX = (int) (img.getWidth() * FACTOR); int scaleY = (int) (img.getHeight() * FACTOR); Image image = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH); BufferedImage buffered = (BufferedImage) image; 不幸的是我得到运行时错误: sun.awt.image.ToolkitImage不能转换为java.awt.image.BufferedImage 显然铸造不起作用。 问题是:什么是(或有)正确的方式转换图像到BufferedImage?

两个列表的所有可能的组合

鉴于我有两个名单: val ints = listOf(0, 1, 2) val strings = listOf(“a”, “b”, “c”) 我想要所有可能的元素组合 0a, 1a, 2a, 0b等 有没有更优雅的方式比: ints.forEach { int -> strings.forEach { string -> println(“$int $string”) } }

不能使用解构声明

我有一个classPerson : class Person(val name: String, val age: Int) 我想用这样的Parent来使用Destructuring声明: val (name, age) = Person(“name”, 22) 但我得到这个错误: Person类的解构声明初始值设定项必须具有“component1()”函数Person类的解构声明初始值设定项必须具有“component2()”函数