Has anyone figured out how to mimic Rust's dbg! ma...
# announcements
p
Has anyone figured out how to mimic Rust's dbg! macro with Kotlin? From Rust docs
Copy code
fn factorial(n: u32) -> u32 {
  if dbg!(n <= 1) {
    dbg!(1)
  } else {
    dbg!(n * factorial(n - 1))
  }
}
I hope to be able to write something like this in Kotlin
Copy code
fun factorial(n: Int): Int {
  if (dbg(n <= 1)) {
    return dbg(1)
  } else {
    return dbg(n * factorial (n - 1))
  }
}
Possible incomplete implementation
Copy code
fun <T> dbg(expr: T): T {
  println(expr)
  return expr
}
Is there any way to capture the expression syntax itself rather than the value?
I have to write my own compiler plugin to do that. Looks like too much work, but it would be fun nonetheless! https://resources.jetbrains.com/storage/products/kotlinconf2018/slides/5_Writing%20Your%20First%20Kotlin%20Compiler%20Plugin.pdf
d
There's an existing compiler plugin that does a very similar thing. I think it's called Kotlin power assert.
y
yep it's
com.bnorm.power.kotlin-power-assert
: https://github.com/bnorm/kotlin-power-assert
👍 1
i
is that is very similar of check function?
c
i have written a library that could be helpful but its only meant for test sources. https://github.com/christophsturm/filepeek
👍 1