我用Kotlin的箭头库玩了一下,发现这个错误就在文档https://arrow-kt.io/docs/optics/中。我做错了什么?
Unresolved reference: company
的代码是下一个,所以它没有编译由于在引用
的错误package com.app
import arrow.optics.Optional
import arrow.optics.optics
@optics
data class Street(val number: Int, val name: String) {
companion object
}
@optics
data class Address(val city: String, val street: Street) {
companion object
}
@optics
data class Company(val name: String, val address: Address) {
companion object
}
@optics
data class Employee(val name: String, val company: Company) {
companion object
}
fun main() {
// an immutable value with very nested components
val john = Employee("John Doe", Company("Kategory", Address("Functional city", Street(42, "lambda street"))))
// an Optional points to one place in the value
val optional: Optional<Employee, String> = Employee.company.address.street.name
// and now you can modify into a new copy without nested 'copy's!
optional.modify(john, String::toUpperCase)
}
my dependencies are next
//region Arrow
implementation("io.arrow-kt:arrow-core:$arrow_version")
implementation("io.arrow-kt:arrow-fx-coroutines:$arrow_version")
implementation("io.arrow-kt:arrow-optics:$arrow_version")
//endregion
您的Gradle配置似乎缺少一些必需的Google KSP设置。你可以在网站的箭头光学设置部分找到它,以及下面。
plugins {
id("com.google.devtools.ksp") version "$googleKspVersion"
}
dependencies {
ksp("io.arrow-kt:arrow-optics-ksp-plugin:$arrowVersion")
}
您还需要让IDEA意识到生成的源代码,否则它将无法正确地挑选代码以进行代码高亮显示和语法报告。官方Kotlin网站解释了设置,如下所示。
kotlin {
sourceSets.main {
kotlin.srcDir("build/generated/ksp/main/kotlin")
}
sourceSets.test {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
在构建脚本中添加以下代码(*.gradle.kts)。它将注册一个新的源目录,以便您的IDE将看到生成的扩展。不管你有多少种风格和构建类型,这都是有效的。
android {
// ...
androidComponents.onVariants { variant ->
val name = variant.name
sourceSets {
getByName(name).kotlin.srcDir("${buildDir.absolutePath}/generated/ksp/${name}/kotlin")
}
}
}