Hello, everyone! Why the print log `A.b` just show...
# getting-started
l
Hello, everyone! Why the print log
A.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?
f
What are you trying to achieve here?
l
I only want to see a print log like
A
f
you are printing a different thing, it will never be the same
l
Sorry, I want to achieve the
A.b
print log is similar to the print log
A
, just like ‘A.b: xxx@yyyyyyy’.
j
you are probably looking for
java.util.Objects#toIdentityString
, not sure how it works on lambdas though
the default
toString
method does exactly that
k
The reason you're seeing what you're seeing with
A
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.
1
By the way, there is no need to do
"${A.toString()}"
- the idiomatic (and clearer) equivalent is just
"$A"
.
s
The message you're seeing in the second println is actually the exact cause of it.
::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
2
👍 1