Generating lenses

Once Arrow's code generation is configured, you can add the @lenses annotation to the data classes that you want to have lenses generated for:

import arrow.lenses
import arrow.optics.Lens
import arrow.optics.modify

typealias GB = Int

@lenses data class Memory(val size: GB)
@lenses data class MotherBoard(val brand: String, val memory: Memory)
@lenses data class Laptop(val price: Double, val motherBoard: MotherBoard)

fun main(args: Array<String>) {
val laptopX8 = Laptop(500.0, MotherBoard("X", Memory(8)))

val laptopMemorySize: Lens<Laptop, GB> = laptopMotherBoard() compose motherBoardMemory() compose memorySize()

val laptopX16 = laptopMemorySize.modify(laptopPrice().set(laptopX8, 780.0)) { size ->
size * 2
}

println("laptopX16 = $laptopX16")
}

Arrow generates as many lenses as constructor parameters our data classes have, with the name convention classProperty and in the same package, so no extra imports are needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.111