Liberty Tom
10/10/2024, 5:52 AMA.b
just shows me the hint ‘function myCallback (Kotlin reflection is not available)’, I want to achieve the A.b
print log is similar to the print log A
, just like ‘A.b: xxx@yyyyyyy’, how to do it?Fergus Hewson
10/10/2024, 6:25 AMLiberty Tom
10/10/2024, 6:35 AMA
Fergus Hewson
10/10/2024, 6:41 AMLiberty Tom
10/10/2024, 7:19 AMA.b
print log is similar to the print log A
, just like ‘A.b: xxx@yyyyyyy’.John Smith
10/10/2024, 7:55 AMjava.util.Objects#toIdentityString
, not sure how it works on lambdas thoughJohn Smith
10/10/2024, 7:56 AMtoString
method does exactly thatKlitos Kyriacou
10/10/2024, 9:09 AMA
is that A
doesn't have an overridden toString()
method. If you declare it as data object A
it will print just like A.b
does. If you want it to print the identity hashcode instead, do as John Smith advised.Klitos Kyriacou
10/10/2024, 9:11 AM"${A.toString()}"
- the idiomatic (and clearer) equivalent is just "$A"
.Szymon Jeziorski
10/10/2024, 9:38 AM::myCallback
is a function reference.
For computing more accurate `toString`s for it, kotlin uses reflection module.
If you had "org.jetbrains.kotlin:kotlin-reflect" in the classpath, you would see something like this for the second `println`:
A.b: fun myCallback(kotlin.String): kotlin.Unit
This is the exact method that's being invoked for you upon ::myCallback
toString() invocation:
https://github.com/JetBrains/kotlin/blob/4425f814af8dd848d3f1e51f4ffd7b785dd91b03/[…]s/stdlib/jvm/runtime/kotlin/jvm/internal/FunctionReference.java