简体   繁体   English

Kotlin 光学数据 class 参考中未解决的参考

[英]Unresolved reference in Kotlin optic data class reference

Was playing a bit with arrow library for Kotlin and found this error right out of the documentation https://arrow-kt.io/docs/optics/ .正在使用 Kotlin 的箭头库并在文档https://arrow-kt.io/docs/optics/中发现了这个错误。 What am I doing wrong?我究竟做错了什么?

 Unresolved reference: company

the code is next, so it is not compiling due to an error in reference代码是下一个,因此由于引用错误而无法编译

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

Your Gradle configuration seems to be missing some of the required Google KSP setup.您的 Gradle 配置似乎缺少一些必需的 Google KSP 设置。 You can find it in the Arrow Optics Setup section of the website, and below.您可以在网站的Arrow 光学设置部分及下方找到它。

plugins {
    id("com.google.devtools.ksp") version "$googleKspVersion"
}

dependencies {
    ksp("io.arrow-kt:arrow-optics-ksp-plugin:$arrowVersion")
}

You also need to make IDEA aware of the generated sources, or it will not be able to correctly pick up the code for code highlighting and syntax reporting.您还需要让 IDEA 了解生成的源代码,否则它将无法正确拾取代码以进行代码突出显示和语法报告。 The setup is explained on the official Kotlin Website , and shown below.设置在Kotlin 官方网站上进行了说明,如下所示。

kotlin {
    sourceSets.main {
        kotlin.srcDir("build/generated/ksp/main/kotlin")
    }
    sourceSets.test {
        kotlin.srcDir("build/generated/ksp/test/kotlin")
    }
}

Add the code below (*.gradle.kts) in your build script.在构建脚本中添加以下代码 (*.gradle.kts)。 It will register a new source dir so that your IDE will see generated extensions.它将注册一个新的源目录,以便您的 IDE 将看到生成的扩展。 This works regardless of the number of flavors and build types you have.无论您拥有多少口味和构建类型,这都有效。

android {
    // ...
    androidComponents.onVariants { variant ->
        val name = variant.name
        sourceSets {
            getByName(name).kotlin.srcDir("${buildDir.absolutePath}/generated/ksp/${name}/kotlin")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM