https://kotlinlang.org logo
#arrow
Title
# arrow
l

Luke

07/12/2022, 6:28 PM
In optics, is there a way to "tap" on foci? I want to log stuff for debugging purposes. I'm searching for something like:
Copy code
MyClass.a.b.c.tap { println(it) }.d.e.f.set(state, newValue)
s

simon.vergauwen

07/13/2022, 8:53 AM
Hey @Luke, Something like that is not possible, you could however split this up a bit.
Copy code
val pathC = MyClass.a.b.c
val pathF = pathC.d.e.f

pathC.get(state).also(::println)
pathF.set(state, newValue)
p

pakoito

07/13/2022, 10:46 AM
puts on golf cap
Copy code
MyClass.a.b.c.also { println(get(state)) }.d.e.f.set(state, newValue)
🤯 2
🔝 3
l

Luke

07/13/2022, 1:10 PM
That would work nicely thank you @pakoito. I didn't think of that. @simon.vergauwen, I'm curious about why it would be impossible though... Do you mean it's impossible right now because there is no function implemented in the library for it? Because I wrote this that seems to work fine:
Copy code
inline fun <T, U> Every<T, U>.tap(crossinline block: (U) -> Unit): Every<T, U> = plus(
    Iso(
        get = { it.also(block) },
        reverseGet = ::identity
    )
)
But for my debugging, yes, it's overkill