我试图了解reified
关键字的目的,显然它允许我们对泛型进行反思.
但是,当我把它放在外面时它的效果一样好.任何人都在关心何时产生实际差异?
如何将我的Kotlin转换Array
为varargs Java String[]
?
val angularRoutings =
arrayOf<String>("/language", "/home")
// this doesn't work
web.ignoring().antMatchers(angularRoutings)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚?:
例如这种情况
val list = mutableList ?: mutableListOf()
Run Code Online (Sandbox Code Playgroud)
为什么可以修改它
val list = if (mutableList != null) mutableList else mutableListOf()
Run Code Online (Sandbox Code Playgroud) 在Java中,您可以执行以下操作:
class MyClass extends SuperClass implements MyInterface, ...
Run Code Online (Sandbox Code Playgroud)
可以在Kotlin做同样的事情吗?假设SuperClass
是抽象的并且没有实现MyInterface
如何使用Kotlin在Spring Boot中正确初始化ConfigurationProperties ?
目前我喜欢以下示例:
@ConfigurationProperties("app")
class Config {
var foo: String? = null
}
Run Code Online (Sandbox Code Playgroud)
但它看起来很丑陋,实际上foo
并不是一个var
可行的,foo是不变的 val
,应该在启动时初始化,并且将来不会改变.
switch
在Kotlin 的替代品是when
.因此,在回收器视图适配器中,当我返回视图类型时,我使用when
:
override fun getItemViewType(position: Int): Int {
when (position) {
0 -> return ItemViewType.TITLE.type
1 -> return ItemViewType.SUBTITLE.type
2 -> return ItemViewType.ITEM.type
else -> return -1
}
}
Run Code Online (Sandbox Code Playgroud)
但是,上述声明发出了警告信息Return can be lifted out of 'when'
.
有谁知道什么是正确的使用方式when
?如何解决上述问题?
我有这样的课
class SomeClass {
fun someFun() {
// ... Some synchronous code
async {
suspendfun()
}
}
private suspend fun suspendFun() {
dependency.otherFun().await()
// ... other code
}
}
Run Code Online (Sandbox Code Playgroud)
我想进行单元测试,someFun()
所以我编写了一个单元测试,如下所示:
@Test
fun testSomeFun() {
runBlocking {
someClass.someFun()
}
// ... verifies & asserts
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用,因为runBlocking实际上不会阻止执行,直到runBlocking内的所有内容都完成.如果我suspendFun()
直接在里面测试runBlocking
它按预期工作,但我希望能够一起测试someFun()
.
有关如何使用同步和异步代码测试函数的任何线索?
为了描述Gradle构建脚本,我们可以使用Kotlin via build.gradle.kts
文件.全局定义要使用的 Kotlin版本是常见的问题,无论是dependencies
在构建plugin
部分还是在构建部分中(对于给定的案例,使用不同的版本是相当罕见的).
请考虑以下代码(Gradle 4.3.1):
plugins {
var pluginVersion = "1.2.30"
kotlin("jvm").version(kotlinVersion)
// more
}
var dependencyVersion = "1.2.30"
dependencies {
compile(kotlin("stdlib", kotlinVersion))
compile(kotlin("reflect", kotlinVersion))
testCompile(kotlin("test", kotlinVersion))
// more
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,kotlin version
(在这种情况下为1.2.30)定义了两次:dependencyVersion
并且pluginVersion
,通常没有区别.由于DSL限制,不可能pluginVersion
从plugins
块外部访问或dependencyVersion
从plugins
块内访问.
如何将版本字符串"1.2.30"
提取到一个地方?
在https://try.kotlinlang.org/#/Kotlin%20Koans/Collections/FlatMap/Task.kt
它有使用flatMap
和的样本map
似乎两者都在做同样的事情,是否有一个样本来显示使用flatMap
和map
?的区别?
数据类型:
data class Shop(val name: String, val customers: List<Customer>)
data class Customer(val name: String, val city: City, val orders: List<Order>) {
override fun toString() = "$name from ${city.name}"
}
data class Order(val products: List<Product>, val isDelivered: Boolean)
data class Product(val name: String, val price: Double) {
override fun toString() = "'$name' for $price"
}
data class City(val name: String) {
override fun toString() = name
}
Run Code Online (Sandbox Code Playgroud)
样品:
fun Shop.getCitiesCustomersAreFrom(): …
Run Code Online (Sandbox Code Playgroud) 我想打印0001(注意前面的3个0),一次增量1,然后达到1000停止.我怎么能在Kotlin那样做,而不用复杂的自己附加0?
以下没有帮助,因为它不会有前面的0.
for (i in 1..1000) print(i)
Run Code Online (Sandbox Code Playgroud) kotlin ×10
java ×2
build.gradle ×1
collections ×1
coroutine ×1
flatmap ×1
generics ×1
gradle ×1
interop ×1
kotlin-reified-type-parameters ×1
spring ×1
spring-boot ×1
unit-testing ×1