Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/ . 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
like image 433
Victor Orlyk Avatar asked Jun 10 '26 19:06

Victor Orlyk


2 Answers

Your Gradle configuration seems to be missing some of the required Google KSP setup. You can find it in the Arrow Optics Setup section of the website, and below.

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. The setup is explained on the official Kotlin Website, and shown below.

kotlin {
    sourceSets.main {
        kotlin.srcDir("build/generated/ksp/main/kotlin")
    }
    sourceSets.test {
        kotlin.srcDir("build/generated/ksp/test/kotlin")
    }
}
like image 183
nomisRev Avatar answered Jun 12 '26 11:06

nomisRev


Add the code below (*.gradle.kts) in your build script. It will register a new source dir so that your IDE will see generated extensions. 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")
        }
    }
}
like image 36
dmitriyzaitsev Avatar answered Jun 12 '26 10:06

dmitriyzaitsev