Hello how to get the current function being execut...
# kotlin-native
a
Hello how to get the current function being executed name I'm mainly developing for iOS i found these by doing some research so it's possible from objc or swift but how to use that in Kotlin native
Copy code
print(__FUNCTION__) // Swift
NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C
print(#function)
j
I'm doing this in some Kotlin multiplatform tests by parsing the method name from the stack trace lines
Throwable().stackTraceToString().lines()
with a regex. The stack trace depth and format varies between platforms, but this should be straightforward if you only need it for iOS. You'll probably need to drop the first couple lines of the stack trace. Then if you know the class name being run in, that helps find the part of the line where the method name starts.
a
Thank you for your response i will try it out and tell you if any thing didn't work as expect 🙏
a
Take a look at this compiler plugin too https://github.com/JakeWharton/cite
j
That's cool. In my case, I actually need the name of the calling function. If your use case is performance sensitive, compile time generation would be way more efficient than generating and parsing a stack trace. It'd be interesting to think if there's a way to get the caller with a combination of compile time generation and runtime access.
a
I've not used cite, but the docs do give an example of fetching the function name so I think that would work
j
Yes,
__MEMBER__
gets the current function name, which sounds like the OP's need. In my case I need the function that called the current function though.
a
ahh okay
a
This is convenient as well but actually my usecase so far is i call get current function name inside an inline function which gives me the name of the function that called the current function
But if cite would give the same result for inline functions that would be so cool
j
I tried cite with an inline function and it still gives the name of the inline function rather than the caller, which makes sense as it's replacing the variables with code generated before compilation. Maybe it's possible the plugin could somehow defer the code replacement until after the function code has been inlined, but before bytecode generation.