https://kotlinlang.org logo
Title
l

louiscad

12/14/2017, 8:19 AM
Hi, what's the most idiomatic way to define and use a custom
toString()
(for debugging purposes) for a java class that doesn't override the one from
Object
?
j

jmfayard

12/14/2017, 8:36 AM
interface Debuggable {
   val debugMsg: String
fun <T : Debuggable> debug(name: String): T {
    println("DEBUG: ${name} = ${debugMsg}")
    return this
}
}
l

louiscad

12/14/2017, 8:45 AM
@jmfayard This is not my class, I can't add an interface to it. I ended up writing using a
print()
extension function that returns what a proper
toString()
should return for now.
k

kingsley

12/14/2017, 8:38 PM
You could also open a proper debugging session, and set breakpoints, so you can see actual values held by the object in question I’m assuming you’re running in an environment where this kind of thing is possible
c

cedric

12/15/2017, 2:18 AM
Hopefully this will become possible if type classes get added to Kotlin (being discussed in a KEEP right now)
👍🏽 1