If I have: ```interface Foo { fun printlog(msg:...
# getting-started
d
If I have:
Copy code
interface Foo {
   fun printlog(msg: String) = "... - $msg"
}

class Bar : Foo
How can I get the printlog in the interface to print out
Bar - some message
? If I use
${this::class.simpleName}
it just gives me the interface's name...
j
Using
${this::class.simpleName}
does work: https://pl.kotl.in/3LrWAHRvp
Copy code
interface Foo {
   fun printlog(msg: String) = "${this::class.simpleName} - $msg"
}

class Bar : Foo

fun main() {
   val log = Bar().printlog("hello")
   println(log)
}
This correctly prints
Bar - hello
I believe you might be calling
printlog
on something that's not an instance of
Bar
maybe?
d
I have:
Copy code
interface BaseResult {
    fun toLogFmt(): String =
        "type=${this::class.simpleName} literal=${toString()}"
}
And:
Copy code
fun interface ResultUseCase2<in Request : Any, out Result : BaseResult> : UseCase<Request, Result> {
    suspend fun doAction(request: Request): Result

    override suspend fun invoke(request: Request): Result =
        doAction(request).also { <http://logger.info|logger.info> { it.toLogFmt() } }
}
And I get
UseCase
instead of the type of result... my results are a sealed interface inherited from BaseResult and data classes inhereted from the sealed interface... I guess my example out there was oversimplified...
j
I guess there must be something else going on, because even when I complicate things a bit more to do what you describe, I still get the correct output: https://pl.kotl.in/LYzJ3k3Vy
Could you share some code that reproduces the problem you're seeing?
j
I fixed the compile errors in your example (which is close to my last example), and it still does work fine: https://pl.kotl.in/3noHQTl-1
d
Oh... It was the logger library printing that 🙃, I guess I was expecting that it should give me the use case's name which is a different problem... I guess that's what happens when one is overworked... Thanks for pointing this out! Side point, do I need kotlin reflection library to use simpleName like that? Is it better to use class.java?
j
I'm glad you figured it out 🙂 I believe you don't need
kotlin-reflect
for
simpleName
.