2

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
Victor Orlyk
  • 1,454
  • 2
  • 15
  • 27

2 Answers2

2

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")
    }
}
nomisRev
  • 1,881
  • 8
  • 15
  • The ksp plugin is listed as optional in the arrow optics setup.. nevertheless when I use that I get: "> Could not find method ksp() for arguments [io.arrow-kt:arrow-optics-ksp-plugin:1.1.2]" Confirmed build/generated folder has no ksp folder in it. – TheJeff Aug 25 '22 at 16:12
  • If it's listed as optional, that is incorrect in the documentation. If you need to rely on `ksp` then it's non-optional. – nomisRev Aug 31 '22 at 12:35
  • 1
    The docs are correct, I got this working. Setting up this project in the parent project doesn't work. Has to be done in child projects and ksp is not required. – TheJeff Aug 31 '22 at 15:37
2

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")
        }
    }
}
dmitriyzaitsev
  • 716
  • 9
  • 17