A way to find out the current function name


Is there a way to find out the name of the current function being run? Something like:

fun someFunc() {
	val currentFuncName = getCurrentFuncName() // how to implement this?

	if (currentFuncName == "someFunc") println("Great! We are in someFunc!")


Depends on target platform. In jdk you can use:

  1. …javaClass.enclosingMethod.name for any object instance produced in your method (function)
  2. Throwable().stackTrace contains it on top
  3. Thread.currentThread().stackTrace contains it

Something similar always presents on other platforms, anyway you have stacktrace even in js.


Thansk, it worked.