Hi! I have a certain dificulty with optics which i...
# arrow
Hi! I have a certain dificulty with optics which is illustrated by example:
Copy code
interface HasAge { val age: Int }

data class Form(
  override val age: Int
): HasAge {
  companion object
// is exported out of the current "module"
val lens: Lens<HasAge, Int> = Form.age // compilation error

// in another module
fun transformAge(form: HasAge, lens: Lens<HasAge, Int>)
Can i somehow generate lens so that it can be assignable in the
val lens
line? Very likely that all this trouble points to the wrong design on my part, but the thing is that full type information is lost between the modules, I cannot export full
data class Form
to be accessible to another module, all I've got is
which I can cast to
and then I thought I'd apply the lens (which also can be exported).
let’s see what happens if we try to write the
Copy code
val lens: Lens<HasAge, Int> = Lens(
  get = { it.age },
  set = { a, i -> /* what should I do here */ }
so you cannot write it only using
now, why doesn’t the assignment to
work? Well, if you write
Lens<HasAge, Int>
that means you should be able to use any
, but your
only works for
my recommendation would be to write something like this:
Copy code
interface HasAge<A> {
  val ageLens: Lens<A, Int>

@optics data class Form(val age: Int) {
  companion object: HasAge<Form> {
    override val ageLens = Form.age
First, thanks for the clarity, I often ask the "stupid" questions, because when in the middle of something you often need a clear view on the problem and that's usually a side-view 🙂 Second, the last sample might be just what I need! I was searching for some way to have a lens and be able to apply it to some generic data of which shape I have a limited knowledge, not concrete and full structure.